forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstring.js
590 lines (536 loc) · 22.2 KB
/
string.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
const assert = require('assert');
const hbs = require('handlebars').create();
const helpers = require('..');
helpers.string({ handlebars: hbs });
describe('string', function() {
describe('changecase', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{changecase foo}}');
assert.equal(fn({ foo: undefined }), '');
});
it('should lowercase a mixed case string', function() {
const fn = hbs.compile('{{changecase "foo Bar Baz"}}');
assert.equal(fn(), 'foo bar baz');
});
it('should lowercase a mixed case string with delimiter string', function() {
const fn = hbs.compile('{{changecase "foo Bar Baz" ":"}}');
assert.equal(fn(), 'foo:bar:baz');
});
it('should lowercase a mixed case string with delimiter function', function() {
// assert.equal(utils.changecase('foo Bar Baz', ch => '-' + ch), 'foo-bar-baz');
});
it('should lowercase a single character', function() {
assert.equal(hbs.compile('{{changecase "f"}}')(), 'f');
assert.equal(hbs.compile('{{changecase "A"}}')(), 'a');
});
});
describe('append', function() {
it('should append the second string to the first', function() {
const fn = hbs.compile('{{append value ".html"}}');
assert.equal(fn({value: 'file'}), 'file.html');
});
it('should return the first if no second string was passed', function() {
const fn = hbs.compile('{{append value}}');
assert.equal(fn({value: 'file'}), 'file');
});
});
describe('camelcase', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{camelcase}}');
assert.equal(fn(), '');
});
it('should return the string in camelcase', function() {
const fn = hbs.compile('{{camelcase "foo bar baz qux"}}');
assert.equal(fn(), 'fooBarBazQux');
});
it('should lowercase a single character', function() {
assert.equal(hbs.compile('{{camelcase "f"}}')(), 'f');
assert.equal(hbs.compile('{{camelcase "A"}}')(), 'a');
});
});
describe('capitalize', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{capitalize}}');
assert.equal(fn(), '');
});
it('should capitalize a word.', function() {
const fn = hbs.compile('{{capitalize "foo"}}');
assert.equal(fn(), 'Foo');
});
});
describe('capitalizeAll', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{capitalizeAll}}');
assert.equal(fn(), '');
});
it('should return the string with the every word capitalized.', function() {
const fn = hbs.compile('{{capitalizeAll "bender should not bE allowed on tV"}}');
assert.equal(fn(), 'Bender Should Not BE Allowed On TV');
});
});
describe('center', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{center}}');
assert.equal(fn(), '');
});
it('should return the string centered by using non-breaking spaces.', function() {
const fn = hbs.compile('{{center "Bender should not be allowed on tv." 2}}');
assert.equal(fn(), '  Bender should not be allowed on tv.  ');
});
});
describe('chop', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{chop}}');
assert.equal(fn(), '');
});
it('should remove non-word characters from start of string', function() {
const fn = hbs.compile('{{chop "- foo bar baz"}}');
assert.equal(fn(), 'foo bar baz');
});
it('should remove non-word characters from end of string', function() {
const fn = hbs.compile('{{chop "foo bar baz _- "}}');
assert.equal(fn(), 'foo bar baz');
});
});
describe('dashcase', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{dashcase}}');
assert.equal(fn(), '');
});
it('should return the string in dashcase', function() {
const fn = hbs.compile('{{dashcase "foo bar baz qux"}}');
assert.equal(fn(), 'foo-bar-baz-qux');
});
it('should lowercase a single character', function() {
assert.equal(hbs.compile('{{dashcase "f"}}')(), 'f');
assert.equal(hbs.compile('{{dashcase "A"}}')(), 'a');
});
});
describe('dotcase', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{dotcase}}');
assert.equal(fn(), '');
});
it('should return the string in dotcase', function() {
const fn = hbs.compile('{{dotcase "foo bar baz qux"}}');
assert.equal(fn(), 'foo.bar.baz.qux');
});
it('should return the example string in dotcase', function() {
const fn = hbs.compile('{{dotcase "a-b-c d_e"}}');
assert.equal(fn(), 'a.b.c.d.e');
});
it('should lowercase a single character', function() {
assert.equal(hbs.compile('{{dotcase "f"}}')(), 'f');
assert.equal(hbs.compile('{{dotcase "A"}}')(), 'a');
});
});
describe('ellipsis', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{ellipsis}}');
assert.equal(fn(), '');
});
it('should return the string truncated by a specified length.', function() {
const fn = hbs.compile('{{ellipsis "Bender should not be allowed on tv." 31}}');
assert.equal(fn(), 'Bender should not be allowed on…');
});
it('should return the string if shorter than the specified length.', function() {
const fn = hbs.compile('{{ellipsis "Bender should not be allowed on tv." 100}}');
assert.equal(fn(), 'Bender should not be allowed on tv.');
});
});
describe('hyphenate', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{hyphenate}}');
assert.equal(fn(), '');
});
it('should return the string with spaces replaced with hyphens.', function() {
const fn = hbs.compile('{{hyphenate "Bender should not be allowed on tv."}}');
assert.equal(fn(), 'Bender-should-not-be-allowed-on-tv.');
});
});
describe('isString', function() {
it('should return true for string', function() {
assert.equal(hbs.compile('{{isString "foo"}}')(), 'true');
});
it('should return true for empty string', function() {
assert.equal(hbs.compile('{{isString ""}}')(), 'true');
});
it('should return false for number', function() {
assert.equal(hbs.compile('{{isString 123}}')(), 'false');
});
it('should return false for null', function() {
assert.equal(hbs.compile('{{isString null}}')(), 'false');
});
it('should return false when undefined', function() {
assert.equal(hbs.compile('{{isString}}')(), 'false');
});
});
describe('lowercase', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{lowercase}}');
assert.equal(fn(), '');
});
it('should return the string in lowercase', function() {
const fn = hbs.compile('{{lowercase "BENDER SHOULD NOT BE ALLOWED ON TV"}}');
assert.equal(fn(), 'bender should not be allowed on tv');
});
});
describe('occurrences', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{occurrences}}');
assert.equal(fn(), '');
});
it('should return the number of occurrences of a string, within a string.', function() {
const fn = hbs.compile('{{occurrences "Jar-Jar Binks" "Jar"}}');
assert.equal(fn(), '2');
});
});
describe('pascalcase', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{pascalcase}}');
assert.equal(fn(), '');
});
it('should return the string in pascalcase', function() {
const fn = hbs.compile('{{pascalcase "foo bar baz qux"}}');
assert.equal(fn(), 'FooBarBazQux');
});
it('should uppercase a single character', function() {
assert.equal(hbs.compile('{{pascalcase "f"}}')(), 'F');
assert.equal(hbs.compile('{{pascalcase "A"}}')(), 'A');
});
});
describe('pathcase', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{pathcase}}');
assert.equal(fn(), '');
});
it('should return the string in pathcase', function() {
const fn = hbs.compile('{{pathcase "foo bar baz qux"}}');
assert.equal(fn(), 'foo/bar/baz/qux');
});
it('should lowercase a single character', function() {
assert.equal(hbs.compile('{{pathcase "f"}}')(), 'f');
assert.equal(hbs.compile('{{pathcase "A"}}')(), 'a');
});
});
describe('plusify', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{plusify}}');
assert.equal(fn(), '');
});
it('should return the empty string with no change.', function() {
const fn = hbs.compile('{{plusify ""}}');
assert.equal(fn(), '');
});
it('should return the string with no change.', function() {
const fn = hbs.compile('{{plusify "BenderShouldNotBeAllowedOnTv."}}');
assert.equal(fn(), 'BenderShouldNotBeAllowedOnTv.');
});
it('should return the string with spaces replaced with pluses.', function() {
const fn = hbs.compile('{{plusify "Bender should not be allowed on tv."}}');
assert.equal(fn(), 'Bender+should+not+be+allowed+on+tv.');
});
});
describe('prepend', function() {
it('should prepend a string with another string', function() {
const fn = hbs.compile('{{prepend value "foo-"}}');
assert.equal(fn({ value: 'bar' }), 'foo-bar');
});
it('should return the original string if no prepend value was passed', function() {
const fn = hbs.compile('{{prepend value}}');
assert.equal(fn({ value: 'bar' }), 'bar');
});
});
// describe('raw', function() {
// it('should render the inner block without processing', function() {
// const fn = hbs.compile('{{#raw}}{{foo}}{{/raw}}');
// assert.equal(fn(), '{{foo}}');
// });
// });
describe('remove', function() {
it('should remove all instances of one string from another', function() {
const fn = hbs.compile('{{remove "a b a b a b" "a "}}');
assert.equal(fn(), 'b b b');
});
it('should return the original when no second string is specified', function() {
const fn = hbs.compile('{{remove "foo"}}');
assert.equal(fn(), 'foo');
});
it('should return an empty string for invalid parameters', function() {
const fn = hbs.compile('{{remove value}}');
assert.equal(fn({ value: 1 }), '');
});
});
describe('removeFirst', function() {
it('should remove just hte first instance of one string from another', function() {
const fn = hbs.compile('{{removeFirst "a b a b a b" "a "}}');
assert.equal(fn(), 'b a b a b');
});
it('should return the original when no second string is specified', function() {
const fn = hbs.compile('{{removeFirst "foo"}}');
assert.equal(fn(), 'foo');
});
it('should return an empty string for invalid parameters', function() {
const fn = hbs.compile('{{removeFirst value}}');
assert.equal(fn({ value: 1 }), '');
});
});
describe('replace', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{replace}}');
assert.equal(fn(), '');
});
it('should replace occurrences of string "A" with string "B"', function() {
const fn = hbs.compile('{{replace "Bender Bending Rodriguez" "B" "M"}}');
assert.equal(fn(), 'Mender Mending Rodriguez');
});
it('should return the string if `a` is undefined', function() {
const fn = hbs.compile('{{replace "a b c"}}');
assert.equal(fn(), 'a b c');
});
it('should replace the string with `""` if `b` is undefined', function() {
const fn = hbs.compile('{{replace "a b c" "a"}}');
assert.equal(fn(), ' b c');
});
});
describe('replaceFirst', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{replaceFirst}}');
assert.equal(fn(), '');
});
it('should replace the first occurrence of string "A" with string "B"', function() {
const fn = hbs.compile('{{replaceFirst "Bender Bending Rodriguez" "B" "M"}}');
assert.equal(fn(), 'Mender Bending Rodriguez');
});
it('should return the string if `a` is undefined', function() {
const fn = hbs.compile('{{replaceFirst "a b c"}}');
assert.equal(fn(), 'a b c');
});
it('should replace the string with `""` if `b` is undefined', function() {
const fn = hbs.compile('{{replaceFirst "a b c" "a"}}');
assert.equal(fn(), ' b c');
});
});
describe('reverse', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{reverse}}');
assert.equal(fn(), '');
});
it('should return the string in reverse.', function() {
const fn = hbs.compile('{{reverse "bender should NOT be allowed on TV."}}');
assert.equal(fn(), '.VT no dewolla eb TON dluohs redneb');
});
});
describe('sentence', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{sentence}}');
assert.equal(fn(), '');
});
it('should capitalize the first word of each sentence in a string and convert the rest of the sentence to lowercase.', function() {
const fn = hbs.compile('{{sentence "bender should NOT be allowed on TV. fry SHOULD be allowed on TV."}}');
assert.equal(fn(), 'Bender should not be allowed on tv. Fry should be allowed on tv.');
});
});
describe('snakecase', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{snakecase}}');
assert.equal(fn(), '');
});
it('should lowercase a single character', function() {
assert.equal(hbs.compile('{{snakecase "a"}}')(), 'a');
assert.equal(hbs.compile('{{snakecase "A"}}')(), 'a');
});
it('should return the string in snakecase', function() {
const fn = hbs.compile('{{snakecase "foo bar baz qux"}}');
assert.equal(fn(), 'foo_bar_baz_qux');
});
});
describe('split', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{split}}');
assert.equal(fn(), '');
});
it('should split the string with the default character', function() {
const fn = hbs.compile('{{#each (split "a,b,c")}}<{{.}}>{{/each}}');
assert.equal(fn(), '<a><b><c>');
});
it('should split the string on the given character', function() {
const fn = hbs.compile('{{#each (split "a|b|c" "|")}}<{{.}}>{{/each}}');
assert.equal(fn(), '<a><b><c>');
});
});
describe('startsWith', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{startsWith}}');
assert.equal(fn(), '');
});
it('should render "Yes he is", from inside the block.', function() {
const fn = hbs.compile('{{#startsWith "Bender" "Bender is great"}}Yes he is{{/startsWith}}');
assert.equal(fn(), 'Yes he is');
});
it('should render the Inverse block.', function() {
const fn = hbs.compile('{{#startsWith "Goodbye" "Hello, world!"}}Whoops{{else}}Bro, do you even hello world?{{/startsWith}}');
assert.equal(fn(), 'Bro, do you even hello world?');
});
it('should render the Inverse block when an undefined value is passed in..', function() {
const fn = hbs.compile('{{#startsWith "myPrefix" undefined}}fn block{{else}}inverse block{{/startsWith}}');
assert.equal(fn(), 'inverse block');
});
});
describe('titleize', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{titleize}}');
assert.equal(fn(), '');
});
it('should return the string in title case.', function() {
const fn = hbs.compile('{{titleize "Bender-should-Not-be-allowed_on_Tv"}}');
assert.equal(fn(), 'Bender Should Not Be Allowed On Tv');
});
});
describe('trim', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{trim}}');
assert.equal(fn(), '');
});
it('should trim leading whitespace', function() {
const fn = hbs.compile('{{trim " foo"}}');
assert.equal(fn(), 'foo');
});
it('should trim trailing whitespace', function() {
const fn = hbs.compile('{{trim "foo "}}');
assert.equal(fn(), 'foo');
});
});
describe('trimLeft', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{trimLeft}}');
assert.equal(fn(), '');
});
it('should trim leading whitespace', function() {
const fn = hbs.compile('{{trimLeft " foo"}}');
assert.equal(fn(), 'foo');
});
it('should NOT trim trailing whitespace', function() {
const fn = hbs.compile('{{trimLeft "foo "}}');
assert.equal(fn(), 'foo ');
});
});
describe('trimRight', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{trimRight}}');
assert.equal(fn(), '');
});
it('should NOT trim leading whitespace', function() {
const fn = hbs.compile('{{trimRight " foo"}}');
assert.equal(fn(), ' foo');
});
it('should trim trailing whitespace', function() {
const fn = hbs.compile('{{trimRight "foo "}}');
assert.equal(fn(), 'foo');
});
});
describe('truncate', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{truncate}}');
assert.equal(fn(), '');
});
it('should return the string truncated by a specified length.', function() {
const fn = hbs.compile('{{truncate "Bender should not be allowed on tv." 31}}');
assert.equal(fn(), 'Bender should not be allowed on');
});
it('should return the string if shorter than the specified length.', function() {
const fn = hbs.compile('{{truncate "Bender should not be allowed on tv." 100}}');
assert.equal(fn(), 'Bender should not be allowed on tv.');
});
it('should return the string truncated by a specified length', function() {
const fn = hbs.compile('{{truncate "foo bar baz qux" 7}}...');
assert.equal(fn(), 'foo bar...');
});
it('should return the string truncated by a specified length, providing a custom string to denote an omission.', function() {
const fn = hbs.compile('{{truncate "foo bar baz qux" 7 "…"}}');
assert.equal(fn(), 'foo ba…');
});
it('should return the string truncated from the left with a negative length', function() {
const fn = hbs.compile('{{truncate "foo bar baz qux" -3}}');
assert.equal(fn(), 'qux');
});
});
describe('truncateWords', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{truncateWords}}');
assert.equal(fn(), '');
});
it('should return the string truncated by a specified number of words.', function() {
const fn = hbs.compile('{{truncateWords "Bender should not be allowed on tv." 3}}');
assert.equal(fn(), 'Bender should not…');
});
it('should return the string if shorter than the specified number of words.', function() {
const fn = hbs.compile('{{truncateWords "Bender should not be allowed on tv." 100}}');
assert.equal(fn(), 'Bender should not be allowed on tv.');
});
it('should return the string truncated by a specified number of words with the custom suffix.', function() {
const fn = hbs.compile('{{truncateWords "foo bar baz qux" 3 ""}}');
assert.equal(fn(), 'foo bar baz');
});
it('should return the string truncated by a specified number of words with the custom suffix.', function() {
const fn = hbs.compile('{{truncateWords "foo bar baz qux" 2 " [see more]"}}');
assert.equal(fn(), 'foo bar [see more]');
});
});
describe('uppercase', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{uppercase}}');
assert.equal(fn(), '');
});
it('should return the string in uppercase', function() {
const fn = hbs.compile('{{uppercase "bender should not be allowed on tv"}}');
assert.equal(fn(), 'BENDER SHOULD NOT BE ALLOWED ON TV');
});
it('should work as a block helper', function() {
const fn = hbs.compile('{{#uppercase}}bender should not be allowed on tv{{/uppercase}}');
assert.equal(fn(), 'BENDER SHOULD NOT BE ALLOWED ON TV');
});
});
describe('slashToDot', function() {
it('should return an empty string if undefined', function() {
const fn = hbs.compile('{{slashToDot}}');
assert.equal(fn(), '');
});
it('should replace slashes with dots', function() {
const fn = hbs.compile('{{slashToDot "one/two / three\\four"}}');
assert.equal(fn(), 'one.two.three.four');
});
it('should replace multiple slashes correctly', function() {
const fn = hbs.compile('{{slashToDot "one/two/three/four"}}');
assert.equal(fn(), 'one.two.three.four');
});
});
describe('pad', () => {
it('repeats the contents from and to', () => {
const fn = hbs.compile('{{#pad 1 5}}X{{/pad}}');
assert.equal(fn({}), 'XXXX');
});
});
describe('zeroPad', () => {
it('ignores an undefined object', () => {
const fn = hbs.compile('{{zeroPad num}}');
assert.equal(fn({}), '');
});
it('pads a 3-digit number with 1 digit', () => {
const fn = hbs.compile('{{zeroPad num}}');
assert.equal(fn({ num: 123 }), '0123');
});
it('doesn\'t pad a 3-digit number when not necessary', () => {
const fn = hbs.compile('{{zeroPad num 1}}');
assert.equal(fn({ num: 123 }), '123');
});
it('pads to the right amount', () => {
const fn = hbs.compile('{{zeroPad num 6}}');
assert.equal(fn({ num: 123 }), '000123');
});
it('pads a number in string', () => {
const fn = hbs.compile('{{zeroPad num 3}}');
assert.equal(fn({ num: '5' }), '005');
});
});
});