-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
string.js
379 lines (345 loc) · 14.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
'use strict';
require('mocha');
var assert = require('assert');
var hbs = require('handlebars').create();
var helpers = require('..');
helpers.string({handlebars: hbs});
describe('string', function() {
describe('camelcase', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{camelcase}}');
assert.equal(fn(), '');
});
it('should return the string in camelcase', function() {
var 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() {
var fn = hbs.compile('{{capitalize}}');
assert.equal(fn(), '');
});
it('should capitalize a word.', function() {
var fn = hbs.compile('{{capitalize "foo"}}');
assert.equal(fn(), 'Foo');
});
});
describe('capitalizeAll', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{capitalizeAll}}');
assert.equal(fn(), '');
});
it('should return the string with the every word capitalized.', function() {
var 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() {
var fn = hbs.compile('{{center}}');
assert.equal(fn(), '');
});
it('should return the string centered by using non-breaking spaces.', function() {
var 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() {
var fn = hbs.compile('{{chop}}');
assert.equal(fn(), '');
});
it('should remove non-word characters from start of string', function() {
var fn = hbs.compile('{{chop "- foo bar baz"}}');
assert.equal(fn(), 'foo bar baz');
});
it('should remove non-word characters from end of string', function() {
var 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() {
var fn = hbs.compile('{{dashcase}}');
assert.equal(fn(), '');
});
it('should return the string in dashcase', function() {
var 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() {
var fn = hbs.compile('{{dotcase}}');
assert.equal(fn(), '');
});
it('should return the string in dotcase', function() {
var fn = hbs.compile('{{dotcase "foo bar baz qux"}}');
assert.equal(fn(), 'foo.bar.baz.qux');
});
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() {
var fn = hbs.compile('{{ellipsis}}');
assert.equal(fn(), '');
});
it('should return then string truncated by a specified length.', function() {
var 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() {
var 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() {
var fn = hbs.compile('{{hyphenate}}');
assert.equal(fn(), '');
});
it('should return the string with spaces replaced with hyphens.', function() {
var 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() {
var fn = hbs.compile('{{lowercase}}');
assert.equal(fn(), '');
});
it('should return the string in lowercase', function() {
var 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() {
var fn = hbs.compile('{{occurrences}}');
assert.equal(fn(), '');
});
it('should return the number of occurrences of a string, within a string.', function() {
var fn = hbs.compile('{{occurrences "Jar-Jar Binks" "Jar"}}');
assert.equal(fn(), '2');
});
});
describe('pascalcase', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{pascalcase}}');
assert.equal(fn(), '');
});
it('should return the string in pascalcase', function() {
var 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() {
var fn = hbs.compile('{{pathcase}}');
assert.equal(fn(), '');
});
it('should return the string in pathcase', function() {
var 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() {
var fn = hbs.compile('{{plusify}}');
assert.equal(fn(), '');
});
it('should return the empty string with no change.', function() {
var fn = hbs.compile('{{plusify ""}}');
assert.equal(fn(), '');
});
it('should return the string with no change.', function() {
var fn = hbs.compile('{{plusify "BenderShouldNotBeAllowedOnTv."}}');
assert.equal(fn(), 'BenderShouldNotBeAllowedOnTv.');
});
it('should return the string with spaces replaced with pluses.', function() {
var fn = hbs.compile('{{plusify "Bender should not be allowed on tv."}}');
assert.equal(fn(), 'Bender+should+not+be+allowed+on+tv.');
});
});
describe('replace', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{replace}}');
assert.equal(fn(), '');
});
it('should replace occurrences of string "A" with string "B"', function() {
var 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() {
var fn = hbs.compile('{{replace "a b c"}}');
assert.equal(fn(), 'a b c');
});
it('should replace the string with `""` if `b` is undefined', function() {
var fn = hbs.compile('{{replace "a b c" "a"}}');
assert.equal(fn(), ' b c');
});
});
describe('reverse', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{reverse}}');
assert.equal(fn(), '');
});
it('should return the string in reverse.', function() {
var 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() {
var 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() {
var 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() {
var 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() {
var 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() {
var fn = hbs.compile('{{split}}');
assert.equal(fn(), '');
});
it('should split the string with the default character', function() {
var 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() {
var 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() {
var fn = hbs.compile('{{startsWith}}');
assert.equal(fn(), '');
});
it('should render "Yes he is", from inside the block.', function() {
var 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() {
var 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() {
var 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() {
var fn = hbs.compile('{{titleize}}');
assert.equal(fn(), '');
});
it('should return the string in title case.', function() {
var 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() {
var fn = hbs.compile('{{trim}}');
assert.equal(fn(), '');
});
it('should trim leading whitespace', function() {
var fn = hbs.compile('{{trim " foo"}}');
assert.equal(fn(), 'foo');
});
it('should trim trailing whitespace', function() {
var fn = hbs.compile('{{trim "foo "}}');
assert.equal(fn(), 'foo');
});
});
describe('truncate', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{truncate}}');
assert.equal(fn(), '');
});
it('should return the string truncated by a specified length.', function() {
var 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() {
var 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 then string truncated by a specified length', function() {
var fn = hbs.compile('{{truncate "foo bar baz qux" 7}}...');
assert.equal(fn(), 'foo bar...');
});
it('should return then string truncated by a specified length, providing a custom string to denote an omission.', function() {
var fn = hbs.compile('{{truncate "foo bar baz qux" 7 "…"}}');
assert.equal(fn(), 'foo ba…');
});
});
describe('uppercase', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{uppercase}}');
assert.equal(fn(), '');
});
it('should return the string in uppercase', function() {
var 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() {
var fn = hbs.compile('{{#uppercase}}bender should not be allowed on tv{{/uppercase}}');
assert.equal(fn(), 'BENDER SHOULD NOT BE ALLOWED ON TV');
});
});
});