s-string.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. describe('String', function () {
  2. 'use strict';
  3. describe('#trim()', function () {
  4. var mvs = '\u180E';
  5. var mvsIsWS = (/\s/).test(mvs);
  6. var test = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680' + (mvsIsWS ? mvs : '') + '\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' + (mvsIsWS ? mvs : '') + '\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF';
  7. it('trims all ES5 whitespace', function () {
  8. expect(test.trim()).toBe('Hello, World!');
  9. expect(test.trim().length).toBe(13);
  10. });
  11. it('does not trim the zero-width space', function () {
  12. var zws = '\u200b';
  13. expect(zws.trim()).toBe(zws);
  14. });
  15. it('properly handles the mongolian vowel separator', function () {
  16. if (mvsIsWS) {
  17. expect(mvs.trim()).toBe('');
  18. } else {
  19. expect(mvs.trim()).toBe(mvs);
  20. }
  21. });
  22. });
  23. describe('#replace()', function () {
  24. it('returns undefined for non-capturing groups', function () {
  25. var groups = [];
  26. 'x'.replace(/x(.)?/g, function (m, group) {
  27. groups.push(group); /* "" in FF, `undefined` in CH/WK/IE */
  28. });
  29. expect(groups.length).toBe(1);
  30. expect(groups[0]).toBeUndefined();
  31. });
  32. it('should not fail in Firefox', function () {
  33. expect(function () {
  34. return '* alef\n* beth \n* gimel~0\n'.replace(
  35. /(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+([^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm,
  36. function (match, m1, m2, m3, m4) { return '<li>' + m4 + '</li>\n'; }
  37. );
  38. }).not.toThrow();
  39. });
  40. });
  41. describe('#split()', function () {
  42. var test = 'ab';
  43. it('If "separator" is undefined must return Array with one String - "this" string', function () {
  44. expect(test.split()).toEqual([test]);
  45. expect(test.split(void 0)).toEqual([test]);
  46. });
  47. it('If "separator" is undefined and "limit" set to 0 must return Array[]', function () {
  48. expect(test.split(void 0, 0)).toEqual([]);
  49. });
  50. describe('Tests from Steven Levithan', function () {
  51. it("''.split() results in ['']", function () {
  52. expect(''.split()).toEqual(['']);
  53. });
  54. it("''.split(/./) results in ['']", function () {
  55. expect(''.split(/./)).toEqual(['']);
  56. });
  57. it("''.split(/.?/) results in []", function () {
  58. expect(''.split(/.?/)).toEqual([]);
  59. });
  60. it("''.split(/.??/) results in []", function () {
  61. expect(''.split(/.??/)).toEqual([]);
  62. });
  63. it("'ab'.split(/a*/) results in ['', 'b']", function () {
  64. expect('ab'.split(/a*/)).toEqual(['', 'b']);
  65. });
  66. it("'ab'.split(/a*?/) results in ['a', 'b']", function () {
  67. expect('ab'.split(/a*?/)).toEqual(['a', 'b']);
  68. });
  69. it("'ab'.split(/(?:ab)/) results in ['', '']", function () {
  70. expect('ab'.split(/(?:ab)/)).toEqual(['', '']);
  71. });
  72. it("'ab'.split(/(?:ab)*/) results in ['', '']", function () {
  73. expect('ab'.split(/(?:ab)*/)).toEqual(['', '']);
  74. });
  75. it("'ab'.split(/(?:ab)*?/) results in ['a', 'b']", function () {
  76. expect('ab'.split(/(?:ab)*?/)).toEqual(['a', 'b']);
  77. });
  78. it("'test'.split('') results in ['t', 'e', 's', 't']", function () {
  79. expect('test'.split('')).toEqual(['t', 'e', 's', 't']);
  80. });
  81. it("'test'.split() results in ['test']", function () {
  82. expect('test'.split()).toEqual(['test']);
  83. });
  84. it("'111'.split(1) results in ['', '', '', '']", function () {
  85. expect('111'.split(1)).toEqual(['', '', '', '']);
  86. });
  87. it("'test'.split(/(?:)/, 2) results in ['t', 'e']", function () {
  88. expect('test'.split(/(?:)/, 2)).toEqual(['t', 'e']);
  89. });
  90. it("'test'.split(/(?:)/, -1) results in ['t', 'e', 's', 't']", function () {
  91. expect('test'.split(/(?:)/, -1)).toEqual(['t', 'e', 's', 't']);
  92. });
  93. it("'test'.split(/(?:)/, undefined) results in ['t', 'e', 's', 't']", function () {
  94. expect('test'.split(/(?:)/, undefined)).toEqual(['t', 'e', 's', 't']);
  95. });
  96. it("'test'.split(/(?:)/, null) results in []", function () {
  97. expect('test'.split(/(?:)/, null)).toEqual([]);
  98. });
  99. it("'test'.split(/(?:)/, NaN) results in []", function () {
  100. expect('test'.split(/(?:)/, NaN)).toEqual([]);
  101. });
  102. it("'test'.split(/(?:)/, true) results in ['t']", function () {
  103. expect('test'.split(/(?:)/, true)).toEqual(['t']);
  104. });
  105. it("'test'.split(/(?:)/, '2') results in ['t', 'e']", function () {
  106. expect('test'.split(/(?:)/, '2')).toEqual(['t', 'e']);
  107. });
  108. it("'test'.split(/(?:)/, 'two') results in []", function () {
  109. expect('test'.split(/(?:)/, 'two')).toEqual([]);
  110. });
  111. it("'a'.split(/-/) results in ['a']", function () {
  112. expect('a'.split(/-/)).toEqual(['a']);
  113. });
  114. it("'a'.split(/-?/) results in ['a']", function () {
  115. expect('a'.split(/-?/)).toEqual(['a']);
  116. });
  117. it("'a'.split(/-??/) results in ['a']", function () {
  118. expect('a'.split(/-??/)).toEqual(['a']);
  119. });
  120. it("'a'.split(/a/) results in ['', '']", function () {
  121. expect('a'.split(/a/)).toEqual(['', '']);
  122. });
  123. it("'a'.split(/a?/) results in ['', '']", function () {
  124. expect('a'.split(/a?/)).toEqual(['', '']);
  125. });
  126. it("'a'.split(/a??/) results in ['a']", function () {
  127. expect('a'.split(/a??/)).toEqual(['a']);
  128. });
  129. it("'ab'.split(/-/) results in ['ab']", function () {
  130. expect('ab'.split(/-/)).toEqual(['ab']);
  131. });
  132. it("'ab'.split(/-?/) results in ['a', 'b']", function () {
  133. expect('ab'.split(/-?/)).toEqual(['a', 'b']);
  134. });
  135. it("'ab'.split(/-??/) results in ['a', 'b']", function () {
  136. expect('ab'.split(/-??/)).toEqual(['a', 'b']);
  137. });
  138. it("'a-b'.split(/-/) results in ['a', 'b']", function () {
  139. expect('a-b'.split(/-/)).toEqual(['a', 'b']);
  140. });
  141. it("'a-b'.split(/-?/) results in ['a', 'b']", function () {
  142. expect('a-b'.split(/-?/)).toEqual(['a', 'b']);
  143. });
  144. it("'a-b'.split(/-??/) results in ['a', '-', 'b']", function () {
  145. expect('a-b'.split(/-??/)).toEqual(['a', '-', 'b']);
  146. });
  147. it("'a--b'.split(/-/) results in ['a', '', 'b']", function () {
  148. expect('a--b'.split(/-/)).toEqual(['a', '', 'b']);
  149. });
  150. it("'a--b'.split(/-?/) results in ['a', '', 'b']", function () {
  151. expect('a--b'.split(/-?/)).toEqual(['a', '', 'b']);
  152. });
  153. it("'a--b'.split(/-??/) results in ['a', '-', '-', 'b']", function () {
  154. expect('a--b'.split(/-??/)).toEqual(['a', '-', '-', 'b']);
  155. });
  156. it("''.split(/()()/) results in []", function () {
  157. expect(''.split(/()()/)).toEqual([]);
  158. });
  159. it("'.'.split(/()()/) results in ['.']", function () {
  160. expect('.'.split(/()()/)).toEqual(['.']);
  161. });
  162. it("'.'.split(/(.?)(.?)/) results in ['', '.', '', '']", function () {
  163. expect('.'.split(/(.?)(.?)/)).toEqual(['', '.', '', '']);
  164. });
  165. it("'.'.split(/(.??)(.??)/) results in ['.']", function () {
  166. expect('.'.split(/(.??)(.??)/)).toEqual(['.']);
  167. });
  168. it("'.'.split(/(.)?(.)?/) results in ['', '.', undefined, '']", function () {
  169. expect('.'.split(/(.)?(.)?/)).toEqual(['', '.', undefined, '']);
  170. });
  171. it("'A<B>bold</B>and<CODE>coded</CODE>'.split(/<(\\/)?([^<>]+)>/) results in ['A', undefined, 'B', 'bold', '/', 'B', 'and', undefined, 'CODE', 'coded', '/', 'CODE', '']", function () {
  172. expect('A<B>bold</B>and<CODE>coded</CODE>'.split(/<(\/)?([^<>]+)>/)).toEqual(['A', undefined, 'B', 'bold', '/', 'B', 'and', undefined, 'CODE', 'coded', '/', 'CODE', '']);
  173. });
  174. it("'tesst'.split(/(s)*/) results in ['t', undefined, 'e', 's', 't']", function () {
  175. expect('tesst'.split(/(s)*/)).toEqual(['t', undefined, 'e', 's', 't']);
  176. });
  177. it("'tesst'.split(/(s)*?/) results in ['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't']", function () {
  178. expect('tesst'.split(/(s)*?/)).toEqual(['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't']);
  179. });
  180. it("'tesst'.split(/(s*)/) results in ['t', '', 'e', 'ss', 't']", function () {
  181. expect('tesst'.split(/(s*)/)).toEqual(['t', '', 'e', 'ss', 't']);
  182. });
  183. it("'tesst'.split(/(s*?)/) results in ['t', '', 'e', '', 's', '', 's', '', 't']", function () {
  184. expect('tesst'.split(/(s*?)/)).toEqual(['t', '', 'e', '', 's', '', 's', '', 't']);
  185. });
  186. it("'tesst'.split(/(?:s)*/) results in ['t', 'e', 't']", function () {
  187. expect('tesst'.split(/(?:s)*/)).toEqual(['t', 'e', 't']);
  188. });
  189. it("'tesst'.split(/(?=s+)/) results in ['te', 's', 'st']", function () {
  190. expect('tesst'.split(/(?=s+)/)).toEqual(['te', 's', 'st']);
  191. });
  192. it("'test'.split('t') results in ['', 'es', '']", function () {
  193. expect('test'.split('t')).toEqual(['', 'es', '']);
  194. });
  195. it("'test'.split('es') results in ['t', 't']", function () {
  196. expect('test'.split('es')).toEqual(['t', 't']);
  197. });
  198. it("'test'.split(/t/) results in ['', 'es', '']", function () {
  199. expect('test'.split(/t/)).toEqual(['', 'es', '']);
  200. });
  201. it("'test'.split(/es/) results in ['t', 't']", function () {
  202. expect('test'.split(/es/)).toEqual(['t', 't']);
  203. });
  204. it("'test'.split(/(t)/) results in ['', 't', 'es', 't', '']", function () {
  205. expect('test'.split(/(t)/)).toEqual(['', 't', 'es', 't', '']);
  206. });
  207. it("'test'.split(/(es)/) results in ['t', 'es', 't']", function () {
  208. expect('test'.split(/(es)/)).toEqual(['t', 'es', 't']);
  209. });
  210. it("'test'.split(/(t)(e)(s)(t)/) results in ['', 't', 'e', 's', 't', '']", function () {
  211. expect('test'.split(/(t)(e)(s)(t)/)).toEqual(['', 't', 'e', 's', 't', '']);
  212. });
  213. it("'.'.split(/(((.((.??)))))/) results in ['', '.', '.', '.', '', '', '']", function () {
  214. expect('.'.split(/(((.((.??)))))/)).toEqual(['', '.', '.', '.', '', '', '']);
  215. });
  216. it("'.'.split(/(((((.??)))))/) results in ['.']", function () {
  217. expect('.'.split(/(((((.??)))))/)).toEqual(['.']);
  218. });
  219. it("'a b c d'.split(/ /, -(Math.pow(2, 32) - 1)) results in ['a']", function () {
  220. expect('a b c d'.split(/ /, -(Math.pow(2, 32) - 1))).toEqual(['a']);
  221. });
  222. it("'a b c d'.split(/ /, Math.pow(2, 32) + 1) results in ['a']", function () {
  223. expect('a b c d'.split(/ /, Math.pow(2, 32) + 1)).toEqual(['a']);
  224. });
  225. it("'a b c d'.split(/ /, Infinity) results in []", function () {
  226. expect('a b c d'.split(/ /, Infinity)).toEqual([]);
  227. });
  228. });
  229. it('works with the second argument', function () {
  230. expect('a b'.split(/ /, 1)).toEqual(['a']);
  231. });
  232. });
  233. describe('#indexOf()', function () {
  234. it('has basic support', function () {
  235. expect('abcab'.indexOf('a')).toBe(0);
  236. expect('abcab'.indexOf('a', 1)).toBe(3);
  237. expect('abcab'.indexOf('a', 4)).toBe(-1);
  238. });
  239. it('works with unicode', function () {
  240. expect('あいabcあいabc'.indexOf('あい')).toBe(0);
  241. expect('あいabcあいabc'.indexOf('あい', 0)).toBe(0);
  242. expect('あいabcあいabc'.indexOf('あい', 1)).toBe(5);
  243. expect('あいabcあいabc'.indexOf('あい', 6)).toBe(-1);
  244. });
  245. });
  246. describe('#lastIndexOf()', function () {
  247. it('has the right length', function () {
  248. expect(String.prototype.lastIndexOf.length).toBe(1);
  249. });
  250. it('has basic support', function () {
  251. expect('abcd'.lastIndexOf('d')).toBe(3);
  252. expect('abcd'.lastIndexOf('d', 3)).toBe(3);
  253. expect('abcd'.lastIndexOf('d', 2)).toBe(-1);
  254. });
  255. it('works with unicode', function () {
  256. expect('abcあい'.lastIndexOf('あい')).toBe(3);
  257. expect('abcあい'.lastIndexOf('あい', 3)).toBe(3);
  258. expect('abcあい'.lastIndexOf('あい', 2)).toBe(-1);
  259. });
  260. });
  261. });