1 /* global describe, it, expect */
3 describe('String', function () {
6 describe('#trim()', function () {
7 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';
9 it('trims all ES5 whitespace', function () {
10 expect(test.trim()).toBe('Hello, World!');
11 expect(test.trim().length).toBe(13);
14 it('does not trim the zero-width space', function () {
15 expect('\u200b'.trim()).toBe('\u200b');
16 expect('\u200b'.trim().length).toBe(1);
20 describe('#replace()', function () {
21 it('returns undefined for non-capturing groups', function () {
23 'x'.replace(/x(.)?/g, function (m, group) {
24 groups.push(group); /* "" in FF, `undefined` in CH/WK/IE */
26 expect(groups.length).toBe(1);
27 expect(groups[0]).toBeUndefined();
30 it('should not fail in Firefox', function () {
32 return '* alef\n* beth \n* gimel~0\n'.replace(
33 /(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+([^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm,
34 function (match, m1, m2, m3, m4) { return '<li>' + m4 + '</li>\n'; }
40 describe('#split()', function () {
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]);
48 it('If "separator" is undefined and "limit" set to 0 must return Array[]', function () {
49 expect(test.split(void 0, 0)).toEqual([]);
52 describe('Tests from Steven Levithan', function () {
53 it("''.split() results in ['']", function () {
54 expect(''.split()).toEqual(['']);
56 it("''.split(/./) results in ['']", function () {
57 expect(''.split(/./)).toEqual(['']);
59 it("''.split(/.?/) results in []", function () {
60 expect(''.split(/.?/)).toEqual([]);
62 it("''.split(/.??/) results in []", function () {
63 expect(''.split(/.??/)).toEqual([]);
65 it("'ab'.split(/a*/) results in ['', 'b']", function () {
66 expect('ab'.split(/a*/)).toEqual(['', 'b']);
68 it("'ab'.split(/a*?/) results in ['a', 'b']", function () {
69 expect('ab'.split(/a*?/)).toEqual(['a', 'b']);
71 it("'ab'.split(/(?:ab)/) results in ['', '']", function () {
72 expect('ab'.split(/(?:ab)/)).toEqual(['', '']);
74 it("'ab'.split(/(?:ab)*/) results in ['', '']", function () {
75 expect('ab'.split(/(?:ab)*/)).toEqual(['', '']);
77 it("'ab'.split(/(?:ab)*?/) results in ['a', 'b']", function () {
78 expect('ab'.split(/(?:ab)*?/)).toEqual(['a', 'b']);
80 it("'test'.split('') results in ['t', 'e', 's', 't']", function () {
81 expect('test'.split('')).toEqual(['t', 'e', 's', 't']);
83 it("'test'.split() results in ['test']", function () {
84 expect('test'.split()).toEqual(['test']);
86 it("'111'.split(1) results in ['', '', '', '']", function () {
87 expect('111'.split(1)).toEqual(['', '', '', '']);
89 it("'test'.split(/(?:)/, 2) results in ['t', 'e']", function () {
90 expect('test'.split(/(?:)/, 2)).toEqual(['t', 'e']);
92 it("'test'.split(/(?:)/, -1) results in ['t', 'e', 's', 't']", function () {
93 expect('test'.split(/(?:)/, -1)).toEqual(['t', 'e', 's', 't']);
95 it("'test'.split(/(?:)/, undefined) results in ['t', 'e', 's', 't']", function () {
96 expect('test'.split(/(?:)/, undefined)).toEqual(['t', 'e', 's', 't']);
98 it("'test'.split(/(?:)/, null) results in []", function () {
99 expect('test'.split(/(?:)/, null)).toEqual([]);
101 it("'test'.split(/(?:)/, NaN) results in []", function () {
102 expect('test'.split(/(?:)/, NaN)).toEqual([]);
104 it("'test'.split(/(?:)/, true) results in ['t']", function () {
105 expect('test'.split(/(?:)/, true)).toEqual(['t']);
107 it("'test'.split(/(?:)/, '2') results in ['t', 'e']", function () {
108 expect('test'.split(/(?:)/, '2')).toEqual(['t', 'e']);
110 it("'test'.split(/(?:)/, 'two') results in []", function () {
111 expect('test'.split(/(?:)/, 'two')).toEqual([]);
113 it("'a'.split(/-/) results in ['a']", function () {
114 expect('a'.split(/-/)).toEqual(['a']);
116 it("'a'.split(/-?/) results in ['a']", function () {
117 expect('a'.split(/-?/)).toEqual(['a']);
119 it("'a'.split(/-??/) results in ['a']", function () {
120 expect('a'.split(/-??/)).toEqual(['a']);
122 it("'a'.split(/a/) results in ['', '']", function () {
123 expect('a'.split(/a/)).toEqual(['', '']);
125 it("'a'.split(/a?/) results in ['', '']", function () {
126 expect('a'.split(/a?/)).toEqual(['', '']);
128 it("'a'.split(/a??/) results in ['a']", function () {
129 expect('a'.split(/a??/)).toEqual(['a']);
131 it("'ab'.split(/-/) results in ['ab']", function () {
132 expect('ab'.split(/-/)).toEqual(['ab']);
134 it("'ab'.split(/-?/) results in ['a', 'b']", function () {
135 expect('ab'.split(/-?/)).toEqual(['a', 'b']);
137 it("'ab'.split(/-??/) results in ['a', 'b']", function () {
138 expect('ab'.split(/-??/)).toEqual(['a', 'b']);
140 it("'a-b'.split(/-/) results in ['a', 'b']", function () {
141 expect('a-b'.split(/-/)).toEqual(['a', 'b']);
143 it("'a-b'.split(/-?/) results in ['a', 'b']", function () {
144 expect('a-b'.split(/-?/)).toEqual(['a', 'b']);
146 it("'a-b'.split(/-??/) results in ['a', '-', 'b']", function () {
147 expect('a-b'.split(/-??/)).toEqual(['a', '-', 'b']);
149 it("'a--b'.split(/-/) results in ['a', '', 'b']", function () {
150 expect('a--b'.split(/-/)).toEqual(['a', '', 'b']);
152 it("'a--b'.split(/-?/) results in ['a', '', 'b']", function () {
153 expect('a--b'.split(/-?/)).toEqual(['a', '', 'b']);
155 it("'a--b'.split(/-??/) results in ['a', '-', '-', 'b']", function () {
156 expect('a--b'.split(/-??/)).toEqual(['a', '-', '-', 'b']);
158 it("''.split(/()()/) results in []", function () {
159 expect(''.split(/()()/)).toEqual([]);
161 it("'.'.split(/()()/) results in ['.']", function () {
162 expect('.'.split(/()()/)).toEqual(['.']);
164 it("'.'.split(/(.?)(.?)/) results in ['', '.', '', '']", function () {
165 expect('.'.split(/(.?)(.?)/)).toEqual(['', '.', '', '']);
167 it("'.'.split(/(.??)(.??)/) results in ['.']", function () {
168 expect('.'.split(/(.??)(.??)/)).toEqual(['.']);
170 it("'.'.split(/(.)?(.)?/) results in ['', '.', undefined, '']", function () {
171 expect('.'.split(/(.)?(.)?/)).toEqual(['', '.', undefined, '']);
173 it("'A<B>bold</B>and<CODE>coded</CODE>'.split(/<(\\/)?([^<>]+)>/) results in ['A', undefined, 'B', 'bold', '/', 'B', 'and', undefined, 'CODE', 'coded', '/', 'CODE', '']", function () {
174 expect('A<B>bold</B>and<CODE>coded</CODE>'.split(/<(\/)?([^<>]+)>/)).toEqual(['A', undefined, 'B', 'bold', '/', 'B', 'and', undefined, 'CODE', 'coded', '/', 'CODE', '']);
176 it("'tesst'.split(/(s)*/) results in ['t', undefined, 'e', 's', 't']", function () {
177 expect('tesst'.split(/(s)*/)).toEqual(['t', undefined, 'e', 's', 't']);
179 it("'tesst'.split(/(s)*?/) results in ['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't']", function () {
180 expect('tesst'.split(/(s)*?/)).toEqual(['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't']);
182 it("'tesst'.split(/(s*)/) results in ['t', '', 'e', 'ss', 't']", function () {
183 expect('tesst'.split(/(s*)/)).toEqual(['t', '', 'e', 'ss', 't']);
185 it("'tesst'.split(/(s*?)/) results in ['t', '', 'e', '', 's', '', 's', '', 't']", function () {
186 expect('tesst'.split(/(s*?)/)).toEqual(['t', '', 'e', '', 's', '', 's', '', 't']);
188 it("'tesst'.split(/(?:s)*/) results in ['t', 'e', 't']", function () {
189 expect('tesst'.split(/(?:s)*/)).toEqual(['t', 'e', 't']);
191 it("'tesst'.split(/(?=s+)/) results in ['te', 's', 'st']", function () {
192 expect('tesst'.split(/(?=s+)/)).toEqual(['te', 's', 'st']);
194 it("'test'.split('t') results in ['', 'es', '']", function () {
195 expect('test'.split('t')).toEqual(['', 'es', '']);
197 it("'test'.split('es') results in ['t', 't']", function () {
198 expect('test'.split('es')).toEqual(['t', 't']);
200 it("'test'.split(/t/) results in ['', 'es', '']", function () {
201 expect('test'.split(/t/)).toEqual(['', 'es', '']);
203 it("'test'.split(/es/) results in ['t', 't']", function () {
204 expect('test'.split(/es/)).toEqual(['t', 't']);
206 it("'test'.split(/(t)/) results in ['', 't', 'es', 't', '']", function () {
207 expect('test'.split(/(t)/)).toEqual(['', 't', 'es', 't', '']);
209 it("'test'.split(/(es)/) results in ['t', 'es', 't']", function () {
210 expect('test'.split(/(es)/)).toEqual(['t', 'es', 't']);
212 it("'test'.split(/(t)(e)(s)(t)/) results in ['', 't', 'e', 's', 't', '']", function () {
213 expect('test'.split(/(t)(e)(s)(t)/)).toEqual(['', 't', 'e', 's', 't', '']);
215 it("'.'.split(/(((.((.??)))))/) results in ['', '.', '.', '.', '', '', '']", function () {
216 expect('.'.split(/(((.((.??)))))/)).toEqual(['', '.', '.', '.', '', '', '']);
218 it("'.'.split(/(((((.??)))))/) results in ['.']", function () {
219 expect('.'.split(/(((((.??)))))/)).toEqual(['.']);
221 it("'a b c d'.split(/ /, -(Math.pow(2, 32) - 1)) results in ['a']", function () {
222 expect('a b c d'.split(/ /, -(Math.pow(2, 32) - 1))).toEqual(['a']);
224 it("'a b c d'.split(/ /, Math.pow(2, 32) + 1) results in ['a']", function () {
225 expect('a b c d'.split(/ /, Math.pow(2, 32) + 1)).toEqual(['a']);
227 it("'a b c d'.split(/ /, Infinity) results in []", function () {
228 expect('a b c d'.split(/ /, Infinity)).toEqual([]);
232 it('works with the second argument', function () {
233 expect('a b'.split(/ /, 1)).toEqual(['a']);
237 describe('#indexOf()', function () {
238 it('has basic support', function () {
239 expect('abcab'.indexOf('a')).toBe(0);
240 expect('abcab'.indexOf('a', 1)).toBe(3);
241 expect('abcab'.indexOf('a', 4)).toBe(-1);
244 it('works with unicode', function () {
245 expect('あいabcあいabc'.indexOf('あい')).toBe(0);
246 expect('あいabcあいabc'.indexOf('あい', 0)).toBe(0);
247 expect('あいabcあいabc'.indexOf('あい', 1)).toBe(5);
248 expect('あいabcあいabc'.indexOf('あい', 6)).toBe(-1);
252 describe('#lastIndexOf()', function () {
253 it('has the right length', function () {
254 expect(String.prototype.lastIndexOf.length).toBe(1);
257 it('has basic support', function () {
258 expect('abcd'.lastIndexOf('d')).toBe(3);
259 expect('abcd'.lastIndexOf('d', 3)).toBe(3);
260 expect('abcd'.lastIndexOf('d', 2)).toBe(-1);
263 it('works with unicode', function () {
264 expect('abcあい'.lastIndexOf('あい')).toBe(3);
265 expect('abcあい'.lastIndexOf('あい', 3)).toBe(3);
266 expect('abcあい'.lastIndexOf('あい', 2)).toBe(-1);