Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80681 views
1
$(document).ready(function() {
2
3
// Include Underscore.string methods to Underscore namespace
4
_.mixin(_.str.exports());
5
6
module("String extensions");
7
8
test("Strings: trim", function() {
9
equals(_.trim(123), "123", "Non string");
10
equals(_(" foo").trim(), "foo");
11
equals(_("foo ").trim(), "foo");
12
equals(_(" foo ").trim(), "foo");
13
equals(_(" foo ").trim(), "foo");
14
equals(_(" foo ", " ").trim(), "foo", "Manually set whitespace");
15
16
equals(_("ffoo").trim("f"), "oo");
17
equals(_("ooff").trim("f"), "oo");
18
equals(_("ffooff").trim("f"), "oo");
19
20
21
equals(_("_-foobar-_").trim("_-"), "foobar");
22
23
equals(_("http://foo/").trim("/"), "http://foo");
24
equals(_("c:\\").trim('\\'), "c:");
25
});
26
27
test("Strings: ltrim", function() {
28
equals(_(" foo").ltrim(), "foo");
29
equals(_(" foo").ltrim(), "foo");
30
equals(_("foo ").ltrim(), "foo ");
31
equals(_(" foo ").ltrim(), "foo ");
32
33
34
equals(_("ffoo").ltrim("f"), "oo");
35
equals(_("ooff").ltrim("f"), "ooff");
36
equals(_("ffooff").ltrim("f"), "ooff");
37
38
equals(_("_-foobar-_").ltrim("_-"), "foobar-_");
39
});
40
41
test("Strings: rtrim", function() {
42
equals(_("http://foo/").rtrim("/"), "http://foo", 'clean trailing slash');
43
equals(_(" foo").rtrim(), " foo");
44
equals(_("foo ").rtrim(), "foo");
45
equals(_("foo ").rtrim(), "foo");
46
equals(_("foo bar ").rtrim(), "foo bar");
47
equals(_(" foo ").rtrim(), " foo");
48
49
equals(_("ffoo").rtrim("f"), "ffoo");
50
equals(_("ooff").rtrim("f"), "oo");
51
equals(_("ffooff").rtrim("f"), "ffoo");
52
53
equals(_("_-foobar-_").rtrim("_-"), "_-foobar");
54
});
55
56
test("Strings: capitalize", function() {
57
equals(_("fabio").capitalize(), "Fabio", 'First letter is upper case');
58
equals(_.capitalize("fabio"), "Fabio", 'First letter is upper case');
59
equals(_(123).capitalize(), "123", "Non string");
60
});
61
62
test("Strings: join", function() {
63
equals(_.join("", "foo", "bar"), "foobar", 'basic join');
64
equals(_.join("", 1, "foo", 2), "1foo2", 'join numbers and strings');
65
equals(_.join(" ","foo", "bar"), "foo bar", 'join with spaces');
66
equals(_.join("1", "2", "2"), "212", 'join number strings');
67
equals(_.join(1, 2, 2), "212", 'join numbers');
68
equals(_(" ").join("foo", "bar"), "foo bar", 'join object oriented');
69
});
70
71
test("Strings: reverse", function() {
72
equals(_.str.reverse("foo"), "oof" );
73
equals(_.str.reverse("foobar"), "raboof" );
74
equals(_.str.reverse("foo bar"), "rab oof" );
75
equals(_.str.reverse("saippuakauppias"), "saippuakauppias" );
76
equals(_.str.reverse(123), "321", "Non string");
77
equals(_.str.reverse(123.45), "54.321", "Non string");
78
});
79
80
test("Strings: clean", function() {
81
equals(_(" foo bar ").clean(), "foo bar");
82
equals(_(123).clean(), "123");
83
});
84
85
test("Strings: sprintf", function() {
86
// Should be very tested function already. Thanks to
87
// http://www.diveintojavascript.com/projects/sprintf-for-javascript
88
equals(_.sprintf("Hello %s", "me"), "Hello me", 'basic');
89
equals(_("Hello %s").sprintf("me"), "Hello me", 'object');
90
equals(_("hello %s").chain().sprintf("me").capitalize().value(), "Hello me", 'Chaining works');
91
equals(_.sprintf("%.1f", 1.22222), "1.2", 'round');
92
equals(_.sprintf("%.1f", 1.17), "1.2", 'round 2');
93
equals(_.sprintf("%(id)d - %(name)s", {id: 824, name: "Hello World"}), "824 - Hello World", 'Named replacements work');
94
equals(_.sprintf("%(args[0].id)d - %(args[1].name)s", {args: [{id: 824}, {name: "Hello World"}]}), "824 - Hello World", 'Named replacements with arrays work');
95
});
96
97
98
test("Strings: vsprintf", function() {
99
equals(_.vsprintf("Hello %s", ["me"]), "Hello me", 'basic');
100
equals(_("Hello %s").vsprintf(["me"]), "Hello me", 'object');
101
equals(_("hello %s").chain().vsprintf(["me"]).capitalize().value(), "Hello me", 'Chaining works');
102
equals(_.vsprintf("%.1f", [1.22222]), "1.2", 'round');
103
equals(_.vsprintf("%.1f", [1.17]), "1.2", 'round 2');
104
equals(_.vsprintf("%(id)d - %(name)s", [{id: 824, name: "Hello World"}]), "824 - Hello World", 'Named replacement works');
105
equals(_.vsprintf("%(args[0].id)d - %(args[1].name)s", [{args: [{id: 824}, {name: "Hello World"}]}]), "824 - Hello World", 'Named replacement with arrays works');
106
});
107
108
test("Strings: startsWith", function() {
109
ok(_("foobar").startsWith("foo"), 'foobar starts with foo');
110
ok(!_("oobar").startsWith("foo"), 'oobar does not start with foo');
111
ok(_(12345).startsWith(123), '12345 starts with 123');
112
ok(!_(2345).startsWith(123), '2345 does not start with 123');
113
});
114
115
test("Strings: endsWith", function() {
116
ok(_("foobar").endsWith("bar"), 'foobar ends with bar');
117
ok(_.endsWith("foobar", "bar"), 'foobar ends with bar');
118
ok(_.endsWith("00018-0000062.Plone.sdh264.1a7264e6912a91aa4a81b64dc5517df7b8875994.mp4", "mp4"), 'endsWith .mp4');
119
ok(!_("fooba").endsWith("bar"), 'fooba does not end with bar');
120
ok(_.endsWith(12345, 45), '12345 ends with 45');
121
ok(!_.endsWith(12345, 6), '12345 does not end with 6');
122
});
123
124
test("Strings: include", function() {
125
ok(_.str.include("foobar", "bar"), 'foobar includes bar');
126
ok(!_.str.include("foobar", "buzz"), 'foobar does not includes buzz');
127
ok(_.str.include(12345, 34), '12345 includes 34');
128
ok(!_.str.contains(12345, 6), '12345 does not includes 6');
129
});
130
131
test('String: chop', function(){
132
ok(_('whitespace').chop(2).length === 5, "output ['wh','it','es','pa','ce']");
133
ok(_('whitespace').chop(3).length === 4, "output ['whi','tes','pac','e']");
134
ok(_('whitespace').chop()[0].length === 10, "output ['whitespace']");
135
ok(_(12345).chop(1).length === 5, "output ['1','2','3','4','5']");
136
});
137
138
test('String: count', function(){
139
equals(_('Hello world').count('l'), 3);
140
equals(_('Hello world').count('Hello'), 1);
141
equals(_('Hello world').count('foo'), 0);
142
equals(_(12345).count(1), 1);
143
equals(_(11345).count(1), 2);
144
});
145
146
test('String: insert', function(){
147
equals(_('Hello ').insert(6, 'Jessy'), 'Hello Jessy');
148
equals(_('Hello ').insert(100, 'Jessy'), 'Hello Jessy');
149
equals(_(12345).insert(6, 'Jessy'), '12345Jessy');
150
});
151
152
test('String: splice', function(){
153
equals(_('https://[email protected]/edtsech/underscore.strings').splice(30, 7, 'epeli'),
154
'https://[email protected]/epeli/underscore.strings');
155
equals(_.splice(12345, 1, 2, 321), '132145', 'Non strings');
156
});
157
158
test('String: succ', function(){
159
equals(_('a').succ(), 'b');
160
equals(_('A').succ(), 'B');
161
equals(_('+').succ(), ',');
162
equals(_(1).succ(), '2');
163
});
164
165
test('String: titleize', function(){
166
equals(_('the titleize string method').titleize(), 'The Titleize String Method');
167
equals(_('the titleize string method').titleize(), 'The Titleize String Method');
168
equals(_(123).titleize(), '123');
169
});
170
171
test('String: camelize', function(){
172
equals(_('the_camelize_string_method').camelize(), 'theCamelizeStringMethod');
173
equals(_('-the-camelize-string-method').camelize(), 'TheCamelizeStringMethod');
174
equals(_('the camelize string method').camelize(), 'theCamelizeStringMethod');
175
equals(_(' the camelize string method').camelize(), 'theCamelizeStringMethod');
176
equals(_('the camelize string method').camelize(), 'theCamelizeStringMethod');
177
equals(_(123).camelize(), '123');
178
});
179
180
test('String: underscored', function(){
181
equals(_('the-underscored-string-method').underscored(), 'the_underscored_string_method');
182
equals(_('theUnderscoredStringMethod').underscored(), 'the_underscored_string_method');
183
equals(_('TheUnderscoredStringMethod').underscored(), 'the_underscored_string_method');
184
equals(_(' the underscored string method').underscored(), 'the_underscored_string_method');
185
equals(_(123).underscored(), '123');
186
});
187
188
test('String: dasherize', function(){
189
equals(_('the_dasherize_string_method').dasherize(), 'the-dasherize-string-method');
190
equals(_('TheDasherizeStringMethod').dasherize(), '-the-dasherize-string-method');
191
equals(_('the dasherize string method').dasherize(), 'the-dasherize-string-method');
192
equals(_('the dasherize string method ').dasherize(), 'the-dasherize-string-method');
193
equals(_(123).dasherize(), '123');
194
});
195
196
test('String: humanize', function(){
197
equals(_('the_humanize_string_method').humanize(), 'The humanize string method');
198
equals(_('ThehumanizeStringMethod').humanize(), 'Thehumanize string method');
199
equals(_('the humanize string method').humanize(), 'The humanize string method');
200
equals(_('the humanize_id string method_id').humanize(), 'The humanize id string method');
201
equals(_('the humanize string method ').humanize(), 'The humanize string method');
202
equals(_(' capitalize dash-CamelCase_underscore trim ').humanize(), 'Capitalize dash camel case underscore trim');
203
equals(_(123).humanize(), '123');
204
});
205
206
test('String: truncate', function(){
207
equals(_('Hello world').truncate(6, 'read more'), 'Hello read more');
208
equals(_('Hello world').truncate(5), 'Hello...');
209
equals(_('Hello').truncate(10), 'Hello');
210
equals(_(1234567890).truncate(5), '12345...');
211
});
212
213
test('String: prune', function(){
214
equals(_('Hello, cruel world').prune(6, ' read more'), 'Hello read more');
215
equals(_('Hello, world').prune(5, 'read a lot more'), 'Hello, world');
216
equals(_('Hello, world').prune(5), 'Hello...');
217
equals(_('Hello, world').prune(8), 'Hello...');
218
equals(_('Hello, cruel world').prune(15), 'Hello, cruel...');
219
equals(_('Hello world').prune(22), 'Hello world');
220
});
221
222
test('String: isBlank', function(){
223
ok(_('').isBlank());
224
ok(_(' ').isBlank());
225
ok(_('\n').isBlank());
226
ok(!_('a').isBlank());
227
ok(!_('0').isBlank());
228
ok(!_(0).isBlank());
229
});
230
231
test('String: escapeHTML', function(){
232
equals(_('<div>Blah & "blah" & \'blah\'</div>').escapeHTML(),
233
'&lt;div&gt;Blah &amp; &quot;blah&quot; &amp; &apos;blah&apos;&lt;/div&gt;');
234
equals(_('&lt;').escapeHTML(), '&amp;lt;');
235
equals(_(5).escapeHTML(), '5');
236
equals(_(undefined).escapeHTML(), '');
237
});
238
239
test('String: unescapeHTML', function(){
240
equals(_('&lt;div&gt;Blah &amp; &quot;blah&quot; &amp; &apos;blah&apos;&lt;/div&gt;').unescapeHTML(),
241
'<div>Blah & "blah" & \'blah\'</div>');
242
equals(_('&amp;lt;').unescapeHTML(), '&lt;');
243
equals(_(5).unescapeHTML(), '5');
244
equals(_(undefined).unescapeHTML(), '');
245
});
246
247
test('String: words', function() {
248
equals(_("I love you!").words().length, 3);
249
equals(_("I_love_you!").words('_').length, 3);
250
equals(_("I-love-you!").words(/-/).length, 3);
251
equals(_(123).words().length, 1);
252
});
253
254
test('String: chars', function() {
255
equals(_("Hello").chars().length, 5);
256
equals(_(123).chars().length, 3);
257
});
258
259
test('String: lines', function() {
260
equals(_("Hello\nWorld").lines().length, 2);
261
equals(_("Hello World").lines().length, 1);
262
equals(_(123).lines().length, 1);
263
});
264
265
test('String: pad', function() {
266
equals(_("1").pad(8), ' 1');
267
equals(_(1).pad(8), ' 1');
268
equals(_("1").pad(8, '0'), '00000001');
269
equals(_("1").pad(8, '0', 'left'), '00000001');
270
equals(_("1").pad(8, '0', 'right'), '10000000');
271
equals(_("1").pad(8, '0', 'both'), '00001000');
272
equals(_("foo").pad(8, '0', 'both'), '000foo00');
273
equals(_("foo").pad(7, '0', 'both'), '00foo00');
274
equals(_("foo").pad(7, '!@$%dofjrofj', 'both'), '!!foo!!');
275
});
276
277
test('String: lpad', function() {
278
equals(_("1").lpad(8), ' 1');
279
equals(_(1).lpad(8), ' 1');
280
equals(_("1").lpad(8, '0'), '00000001');
281
equals(_("1").lpad(8, '0', 'left'), '00000001');
282
});
283
284
test('String: rpad', function() {
285
equals(_("1").rpad(8), '1 ');
286
equals(_(1).lpad(8), ' 1');
287
equals(_("1").rpad(8, '0'), '10000000');
288
equals(_("foo").rpad(8, '0'), 'foo00000');
289
equals(_("foo").rpad(7, '0'), 'foo0000');
290
});
291
292
test('String: lrpad', function() {
293
equals(_("1").lrpad(8), ' 1 ');
294
equals(_(1).lrpad(8), ' 1 ');
295
equals(_("1").lrpad(8, '0'), '00001000');
296
equals(_("foo").lrpad(8, '0'), '000foo00');
297
equals(_("foo").lrpad(7, '0'), '00foo00');
298
equals(_("foo").lrpad(7, '!@$%dofjrofj'), '!!foo!!');
299
});
300
301
test('String: toNumber', function() {
302
deepEqual(_("not a number").toNumber(), Number.NaN);
303
equals(_(0).toNumber(), 0);
304
equals(_("0").toNumber(), 0);
305
equals(_("2.345").toNumber(), 2);
306
equals(_("2.345").toNumber(NaN), 2);
307
equals(_("2.345").toNumber(2), 2.35);
308
equals(_("2.344").toNumber(2), 2.34);
309
equals(_("2").toNumber(2), 2.00);
310
equals(_(2).toNumber(2), 2.00);
311
equals(_(-2).toNumber(), -2);
312
equals(_("-2").toNumber(), -2);
313
});
314
315
test('String: strRight', function() {
316
equals(_("This_is_a_test_string").strRight("_"), "is_a_test_string");
317
equals(_("This_is_a_test_string").strRight("string"), "");
318
equals(_("This_is_a_test_string").strRight(), "This_is_a_test_string");
319
equals(_("This_is_a_test_string").strRight(""), "This_is_a_test_string");
320
equals(_("This_is_a_test_string").strRight("-"), "This_is_a_test_string");
321
equals(_(12345).strRight(2), "345");
322
});
323
324
test('String: strRightBack', function() {
325
equals(_("This_is_a_test_string").strRightBack("_"), "string");
326
equals(_("This_is_a_test_string").strRightBack("string"), "");
327
equals(_("This_is_a_test_string").strRightBack(), "This_is_a_test_string");
328
equals(_("This_is_a_test_string").strRightBack(""), "This_is_a_test_string");
329
equals(_("This_is_a_test_string").strRightBack("-"), "This_is_a_test_string");
330
equals(_(12345).strRightBack(2), "345");
331
});
332
333
test('String: strLeft', function() {
334
equals(_("This_is_a_test_string").strLeft("_"), "This");
335
equals(_("This_is_a_test_string").strLeft("This"), "");
336
equals(_("This_is_a_test_string").strLeft(), "This_is_a_test_string");
337
equals(_("This_is_a_test_string").strLeft(""), "This_is_a_test_string");
338
equals(_("This_is_a_test_string").strLeft("-"), "This_is_a_test_string");
339
equals(_(123454321).strLeft(3), "12");
340
});
341
342
test('String: strLeftBack', function() {
343
equals(_("This_is_a_test_string").strLeftBack("_"), "This_is_a_test");
344
equals(_("This_is_a_test_string").strLeftBack("This"), "");
345
equals(_("This_is_a_test_string").strLeftBack(), "This_is_a_test_string");
346
equals(_("This_is_a_test_string").strLeftBack(""), "This_is_a_test_string");
347
equals(_("This_is_a_test_string").strLeftBack("-"), "This_is_a_test_string");
348
equals(_(123454321).strLeftBack(3), "123454");
349
});
350
351
test('Strings: stripTags', function() {
352
equals(_('a <a href="#">link</a>').stripTags(), 'a link');
353
equals(_('a <a href="#">link</a><script>alert("hello world!")</scr'+'ipt>').stripTags(), 'a linkalert("hello world!")');
354
equals(_('<html><body>hello world</body></html>').stripTags(), 'hello world');
355
equals(_(123).stripTags(), '123');
356
});
357
358
});
359
360