s-string.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. describe('String', function() {
  2. "use strict";
  3. describe("trim", function() {
  4. var test = "\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFFHello, World!\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF";
  5. it('trims all ES5 whitespace', function() {
  6. expect(test.trim()).toEqual("Hello, World!");
  7. expect(test.trim().length).toEqual(13);
  8. });
  9. });
  10. describe("split", function() {
  11. var test = "ab";
  12. it('If "separator" is undefined must return Array with one String - "this" string', function() {
  13. expect(test.split()).toEqual([test]);
  14. expect(test.split(void 0)).toEqual([test]);
  15. });
  16. it('If "separator" is undefined and "limit" set to 0 must return Array[]', function() {
  17. expect(test.split(void 0, 0)).toEqual([]);
  18. });
  19. describe('Tests from Steven Levithan', function () {
  20. it("''.split() results in ['']", function () {
  21. expect(''.split()).toEqual(['']);
  22. });
  23. it("''.split(/./) results in ['']", function () {
  24. expect(''.split(/./)).toEqual(['']);
  25. });
  26. it("''.split(/.?/) results in []", function () {
  27. expect(''.split(/.?/)).toEqual([]);
  28. });
  29. it("''.split(/.??/) results in []", function () {
  30. expect(''.split(/.??/)).toEqual([]);
  31. });
  32. it("'ab'.split(/a*/) results in ['', 'b']", function () {
  33. expect('ab'.split(/a*/)).toEqual(['', 'b']);
  34. });
  35. it("'ab'.split(/a*?/) results in ['a', 'b']", function () {
  36. expect('ab'.split(/a*?/)).toEqual(['a', 'b']);
  37. });
  38. it("'ab'.split(/(?:ab)/) results in ['', '']", function () {
  39. expect('ab'.split(/(?:ab)/)).toEqual(['', '']);
  40. });
  41. it("'ab'.split(/(?:ab)*/) results in ['', '']", function () {
  42. expect('ab'.split(/(?:ab)*/)).toEqual(['', '']);
  43. });
  44. it("'ab'.split(/(?:ab)*?/) results in ['a', 'b']", function () {
  45. expect('ab'.split(/(?:ab)*?/)).toEqual(['a', 'b']);
  46. });
  47. it("'test'.split('') results in ['t', 'e', 's', 't']", function () {
  48. expect('test'.split('')).toEqual(['t', 'e', 's', 't']);
  49. });
  50. it("'test'.split() results in ['test']", function () {
  51. expect('test'.split()).toEqual(['test']);
  52. });
  53. it("'111'.split(1) results in ['', '', '', '']", function () {
  54. expect('111'.split(1)).toEqual(['', '', '', '']);
  55. });
  56. it("'test'.split(/(?:)/, 2) results in ['t', 'e']", function () {
  57. expect('test'.split(/(?:)/, 2)).toEqual(['t', 'e']);
  58. });
  59. it("'test'.split(/(?:)/, -1) results in ['t', 'e', 's', 't']", function () {
  60. expect('test'.split(/(?:)/, -1)).toEqual(['t', 'e', 's', 't']);
  61. });
  62. it("'test'.split(/(?:)/, undefined) results in ['t', 'e', 's', 't']", function () {
  63. expect('test'.split(/(?:)/, undefined)).toEqual(['t', 'e', 's', 't']);
  64. });
  65. it("'test'.split(/(?:)/, null) results in []", function () {
  66. expect('test'.split(/(?:)/, null)).toEqual([]);
  67. });
  68. it("'test'.split(/(?:)/, NaN) results in []", function () {
  69. expect('test'.split(/(?:)/, NaN)).toEqual([]);
  70. });
  71. it("'test'.split(/(?:)/, true) results in ['t']", function () {
  72. expect('test'.split(/(?:)/, true)).toEqual(['t']);
  73. });
  74. it("'test'.split(/(?:)/, '2') results in ['t', 'e']", function () {
  75. expect('test'.split(/(?:)/, '2')).toEqual(['t', 'e']);
  76. });
  77. it("'test'.split(/(?:)/, 'two') results in []", function () {
  78. expect('test'.split(/(?:)/, 'two')).toEqual([]);
  79. });
  80. it("'a'.split(/-/) results in ['a']", function () {
  81. expect('a'.split(/-/)).toEqual(['a']);
  82. });
  83. it("'a'.split(/-?/) results in ['a']", function () {
  84. expect('a'.split(/-?/)).toEqual(['a']);
  85. });
  86. it("'a'.split(/-??/) results in ['a']", function () {
  87. expect('a'.split(/-??/)).toEqual(['a']);
  88. });
  89. it("'a'.split(/a/) results in ['', '']", function () {
  90. expect('a'.split(/a/)).toEqual(['', '']);
  91. });
  92. it("'a'.split(/a?/) results in ['', '']", function () {
  93. expect('a'.split(/a?/)).toEqual(['', '']);
  94. });
  95. it("'a'.split(/a??/) results in ['a']", function () {
  96. expect('a'.split(/a??/)).toEqual(['a']);
  97. });
  98. it("'ab'.split(/-/) results in ['ab']", function () {
  99. expect('ab'.split(/-/)).toEqual(['ab']);
  100. });
  101. it("'ab'.split(/-?/) results in ['a', 'b']", function () {
  102. expect('ab'.split(/-?/)).toEqual(['a', 'b']);
  103. });
  104. it("'ab'.split(/-??/) results in ['a', 'b']", function () {
  105. expect('ab'.split(/-??/)).toEqual(['a', 'b']);
  106. });
  107. it("'a-b'.split(/-/) results in ['a', 'b']", function () {
  108. expect('a-b'.split(/-/)).toEqual(['a', 'b']);
  109. });
  110. it("'a-b'.split(/-?/) results in ['a', 'b']", function () {
  111. expect('a-b'.split(/-?/)).toEqual(['a', 'b']);
  112. });
  113. it("'a-b'.split(/-??/) results in ['a', '-', 'b']", function () {
  114. expect('a-b'.split(/-??/)).toEqual(['a', '-', 'b']);
  115. });
  116. it("'a--b'.split(/-/) results in ['a', '', 'b']", function () {
  117. expect('a--b'.split(/-/)).toEqual(['a', '', 'b']);
  118. });
  119. it("'a--b'.split(/-?/) results in ['a', '', 'b']", function () {
  120. expect('a--b'.split(/-?/)).toEqual(['a', '', 'b']);
  121. });
  122. it("'a--b'.split(/-??/) results in ['a', '-', '-', 'b']", function () {
  123. expect('a--b'.split(/-??/)).toEqual(['a', '-', '-', 'b']);
  124. });
  125. it("''.split(/()()/) results in []", function () {
  126. expect(''.split(/()()/)).toEqual([]);
  127. });
  128. it("'.'.split(/()()/) results in ['.']", function () {
  129. expect('.'.split(/()()/)).toEqual(['.']);
  130. });
  131. it("'.'.split(/(.?)(.?)/) results in ['', '.', '', '']", function () {
  132. expect('.'.split(/(.?)(.?)/)).toEqual(['', '.', '', '']);
  133. });
  134. it("'.'.split(/(.??)(.??)/) results in ['.']", function () {
  135. expect('.'.split(/(.??)(.??)/)).toEqual(['.']);
  136. });
  137. it("'.'.split(/(.)?(.)?/) results in ['', '.', undefined, '']", function () {
  138. expect('.'.split(/(.)?(.)?/)).toEqual(['', '.', undefined, '']);
  139. });
  140. it("'A<B>bold</B>and<CODE>coded</CODE>'.split(/<(\\/)?([^<>]+)>/) results in ['A', undefined, 'B', 'bold', '/', 'B', 'and', undefined, 'CODE', 'coded', '/', 'CODE', '']", function () {
  141. expect('A<B>bold</B>and<CODE>coded</CODE>'.split(/<(\/)?([^<>]+)>/)).toEqual(['A', undefined, 'B', 'bold', '/', 'B', 'and', undefined, 'CODE', 'coded', '/', 'CODE', '']);
  142. });
  143. it("'tesst'.split(/(s)*/) results in ['t', undefined, 'e', 's', 't']", function () {
  144. expect('tesst'.split(/(s)*/)).toEqual(['t', undefined, 'e', 's', 't']);
  145. });
  146. it("'tesst'.split(/(s)*?/) results in ['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't']", function () {
  147. expect('tesst'.split(/(s)*?/)).toEqual(['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't']);
  148. });
  149. it("'tesst'.split(/(s*)/) results in ['t', '', 'e', 'ss', 't']", function () {
  150. expect('tesst'.split(/(s*)/)).toEqual(['t', '', 'e', 'ss', 't']);
  151. });
  152. it("'tesst'.split(/(s*?)/) results in ['t', '', 'e', '', 's', '', 's', '', 't']", function () {
  153. expect('tesst'.split(/(s*?)/)).toEqual(['t', '', 'e', '', 's', '', 's', '', 't']);
  154. });
  155. it("'tesst'.split(/(?:s)*/) results in ['t', 'e', 't']", function () {
  156. expect('tesst'.split(/(?:s)*/)).toEqual(['t', 'e', 't']);
  157. });
  158. it("'tesst'.split(/(?=s+)/) results in ['te', 's', 'st']", function () {
  159. expect('tesst'.split(/(?=s+)/)).toEqual(['te', 's', 'st']);
  160. });
  161. it("'test'.split('t') results in ['', 'es', '']", function () {
  162. expect('test'.split('t')).toEqual(['', 'es', '']);
  163. });
  164. it("'test'.split('es') results in ['t', 't']", function () {
  165. expect('test'.split('es')).toEqual(['t', 't']);
  166. });
  167. it("'test'.split(/t/) results in ['', 'es', '']", function () {
  168. expect('test'.split(/t/)).toEqual(['', 'es', '']);
  169. });
  170. it("'test'.split(/es/) results in ['t', 't']", function () {
  171. expect('test'.split(/es/)).toEqual(['t', 't']);
  172. });
  173. it("'test'.split(/(t)/) results in ['', 't', 'es', 't', '']", function () {
  174. expect('test'.split(/(t)/)).toEqual(['', 't', 'es', 't', '']);
  175. });
  176. it("'test'.split(/(es)/) results in ['t', 'es', 't']", function () {
  177. expect('test'.split(/(es)/)).toEqual(['t', 'es', 't']);
  178. });
  179. it("'test'.split(/(t)(e)(s)(t)/) results in ['', 't', 'e', 's', 't', '']", function () {
  180. expect('test'.split(/(t)(e)(s)(t)/)).toEqual(['', 't', 'e', 's', 't', '']);
  181. });
  182. it("'.'.split(/(((.((.??)))))/) results in ['', '.', '.', '.', '', '', '']", function () {
  183. expect('.'.split(/(((.((.??)))))/)).toEqual(['', '.', '.', '.', '', '', '']);
  184. });
  185. it("'.'.split(/(((((.??)))))/) results in ['.']", function () {
  186. expect('.'.split(/(((((.??)))))/)).toEqual(['.']);
  187. });
  188. it("'a b c d'.split(/ /, -(Math.pow(2, 32) - 1)) results in ['a']", function () {
  189. expect('a b c d'.split(/ /, -(Math.pow(2, 32) - 1))).toEqual(['a']);
  190. });
  191. it("'a b c d'.split(/ /, Math.pow(2, 32) + 1) results in ['a']", function () {
  192. expect('a b c d'.split(/ /, Math.pow(2, 32) + 1)).toEqual(['a']);
  193. });
  194. it("'a b c d'.split(/ /, Infinity) results in []", function () {
  195. expect('a b c d'.split(/ /, Infinity)).toEqual([]);
  196. });
  197. });
  198. });
  199. });