-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstring.js
377 lines (341 loc) · 16 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
/*!
* template-helpers <https://github.com/jonschlinkert/template-helpers>
*
* Copyright (c) 2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
const assert = require('assert');
const template = require('lodash.template');
const helpers = require('..')(['string', 'html']);
const imports = { imports: helpers };
describe('string helpers', function() {
describe('lowercase', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= lowercase() %>', imports)(), '');
});
it('should lower case the characters in a string.', function() {
var fn = template('<%= lowercase("ABC") %>', imports);
assert.equal(fn(), 'abc');
});
});
describe('uppercase', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= uppercase() %>', imports)(), '');
});
it('should upper case the characters in a string.', function() {
var fn = template('<%= uppercase("abc") %>', imports);
assert.equal(fn(), 'ABC');
});
});
describe('trim', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= trim() %>', imports)(), '');
});
it('should strip leading whitespace from a string.', function() {
var fn = template('<%= trim(" abc") %>', imports);
assert.equal(fn(), 'abc');
});
it('should strip trailing whitespace from a string.', function() {
var fn = template('<%= trim("abc ") %>', imports);
assert.equal(fn(), 'abc');
});
});
describe('chop', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= chop() %>', imports)(), '');
});
it('should strip leading whitespace from a string.', function() {
var fn = template('<%= chop(" abc") %>', imports);
assert.equal(fn(), 'abc');
});
it('should strip trailing whitespace from a string.', function() {
var fn = template('<%= chop("abc ") %>', imports);
assert.equal(fn(), 'abc');
});
it('should strip leading non-word characters from a string.', function() {
var fn = template('<%= chop("_-abc") %>', imports);
assert.equal(fn(), 'abc');
});
it('should strip trailing non-word characters from a string.', function() {
var fn = template('<%= chop("abc_-") %>', imports);
assert.equal(fn(), 'abc');
});
});
describe('strip', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= strip() %>', imports)(), '');
});
it('should strip the given substring from a string.', function() {
var fn = template('<%= strip("foo", "foobar") %>', imports);
assert.equal(fn(), 'bar');
});
it('should strip the given regex match from a string.', function() {
var fn = template('<%= strip(/^foo/, "foobarfoo") %>', imports);
assert.equal(fn(), 'barfoo');
});
});
describe('stripIndent', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= stripIndent() %>', imports)(), '');
});
it('should strip indentation from a string.', function() {
var str = template('<%= stripIndent(str) %>', imports)({
str: [' - a', ' - b', ' * c', ' * d', ' + e', ' + f'].join('\n')
});
assert.equal(str, ['- a', '- b', ' * c', ' * d', ' + e', ' + f'].join('\n'));
});
});
describe('camelcase', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= camelcase() %>', imports)(), '');
});
it('should camel-case the characters in a string.', function() {
assert.equal(template('<%= camelcase("foo bar baz") %>', imports)(), 'fooBarBaz');
});
it('should camel-case the characters in a string.', function() {
assert.equal(template('<%= camelcase("A") %>', imports)(), 'a');
});
it('should work with hyphens.', function() {
assert.equal(template('<%= camelcase("foo-bar-baz") %>', imports)(), 'fooBarBaz');
assert.equal(template('<%= camelcase("-foo bar baz-") %>', imports)(), 'fooBarBaz');
});
it('should work with other non-word characters.', function() {
assert.equal(template('<%= camelcase("9foo-bar_baz") %>', imports)(), '9fooBarBaz');
assert.equal(template('<%= camelcase("_foo_bar_baz-") %>', imports)(), 'fooBarBaz');
});
});
describe('pascalcase', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= pascalcase() %>', imports)(), '');
});
it('should camel-case the characters in a string.', function() {
assert.equal(template('<%= pascalcase("a") %>', imports)(), 'A');
assert.equal(template('<%= pascalcase("A") %>', imports)(), 'A');
});
it('should pascal-case the characters in a string.', function() {
assert.equal(template('<%= pascalcase("foo bar baz") %>', imports)(), 'FooBarBaz');
});
it('should work with hyphens.', function() {
assert.equal(template('<%= pascalcase("foo-bar-baz") %>', imports)(), 'FooBarBaz');
assert.equal(template('<%= pascalcase("-foo bar baz-") %>', imports)(), 'FooBarBaz');
});
it('should work with other non-word characters.', function() {
assert.equal(template('<%= pascalcase("9foo-bar_baz") %>', imports)(), '9fooBarBaz');
assert.equal(template('<%= pascalcase("_foo_bar_baz-") %>', imports)(), 'FooBarBaz');
});
});
describe('snakecase', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= snakecase() %>', imports)(), '');
});
it('should camel-case the characters in a string.', function() {
assert.equal(template('<%= snakecase("A") %>', imports)(), 'a');
});
it('should snake-case the characters in a string.', function() {
assert.equal(template('<%= snakecase("foo bar baz") %>', imports)(), 'foo_bar_baz');
});
it('should work with hyphens.', function() {
assert.equal(template('<%= snakecase("foo-bar-baz") %>', imports)(), 'foo_bar_baz');
assert.equal(template('<%= snakecase("-foo bar baz-") %>', imports)(), 'foo_bar_baz');
});
it('should work with other non-word characters.', function() {
assert.equal(template('<%= snakecase("9foo-bar_baz") %>', imports)(), '9foo_bar_baz');
assert.equal(template('<%= snakecase("_foo_bar_baz-") %>', imports)(), 'foo_bar_baz');
});
});
describe('dotcase', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= dotcase() %>', imports)(), '');
});
it('should camel-case the characters in a string.', function() {
assert.equal(template('<%= dotcase("A") %>', imports)(), 'a');
});
it('should dot-case the characters in a string.', function() {
assert.equal(template('<%= dotcase("foo bar baz") %>', imports)(), 'foo.bar.baz');
});
it('should work with hyphens.', function() {
assert.equal(template('<%= dotcase("foo-bar-baz") %>', imports)(), 'foo.bar.baz');
assert.equal(template('<%= dotcase("-foo bar baz-") %>', imports)(), 'foo.bar.baz');
});
it('should work with other non-word characters.', function() {
assert.equal(template('<%= dotcase("9foo-bar_baz") %>', imports)(), '9foo.bar.baz');
assert.equal(template('<%= dotcase("_foo_bar_baz-") %>', imports)(), 'foo.bar.baz');
});
});
describe('dashcase', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= dashcase() %>', imports)(), '');
});
it('should camel-case the characters in a string.', function() {
assert.equal(template('<%= dashcase("A") %>', imports)(), 'a');
});
it('should dash-case the characters in a string.', function() {
assert.equal(template('<%= dashcase("foo bar baz") %>', imports)(), 'foo-bar-baz');
});
it('should work with hyphens.', function() {
assert.equal(template('<%= dashcase("foo-bar-baz") %>', imports)(), 'foo-bar-baz');
assert.equal(template('<%= dashcase("-foo bar baz-") %>', imports)(), 'foo-bar-baz');
});
it('should work with other non-word characters.', function() {
assert.equal(template('<%= dashcase("9foo-bar_baz") %>', imports)(), '9foo-bar-baz');
assert.equal(template('<%= dashcase("_foo_bar_baz-") %>', imports)(), 'foo-bar-baz');
});
});
describe('pathcase', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= pathcase() %>', imports)(), '');
});
it('should camel-case the characters in a string.', function() {
assert.equal(template('<%= pathcase("A") %>', imports)(), 'a');
});
it('should path-case the characters in a string.', function() {
assert.equal(template('<%= pathcase("foo bar baz") %>', imports)(), 'foo/bar/baz');
});
it('should work with hyphens.', function() {
assert.equal(template('<%= pathcase("foo-bar-baz") %>', imports)(), 'foo/bar/baz');
assert.equal(template('<%= pathcase("-foo bar baz-") %>', imports)(), 'foo/bar/baz');
});
it('should work with other non-word characters.', function() {
assert.equal(template('<%= pathcase("9foo-bar_baz") %>', imports)(), '9foo/bar/baz');
assert.equal(template('<%= pathcase("_foo_bar_baz-") %>', imports)(), 'foo/bar/baz');
});
});
describe('sentencecase', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= sentencecase() %>', imports)(), '');
});
it('should camel-case the characters in a string.', function() {
assert.equal(template('<%= sentencecase("A") %>', imports)(), 'A');
assert.equal(template('<%= sentencecase("a") %>', imports)(), 'A');
});
it('should sentence-case the characters in a string.', function() {
assert.equal(template('<%= sentencecase("foo bar baz.") %>', imports)(), 'Foo bar baz.');
assert.equal(template('<%= sentencecase("a") %>', imports)(), 'A');
});
});
describe('hyphenate', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= hyphenate() %>', imports)(), '');
});
it('should hyphenate the characters in a string.', function() {
assert.equal(template('<%= hyphenate("foo bar baz") %>', imports)(), 'foo-bar-baz');
});
it('should work with hyphens.', function() {
assert.equal(template('<%= hyphenate("foo-bar-baz") %>', imports)(), 'foo-bar-baz');
assert.equal(template('<%= hyphenate("-foo bar baz-") %>', imports)(), 'foo-bar-baz');
});
it('should work with other non-word characters.', function() {
assert.equal(template('<%= hyphenate("9foo-bar_baz") %>', imports)(), '9foo-bar_baz');
assert.equal(template('<%= hyphenate("_foo_bar_baz-") %>', imports)(), 'foo_bar_baz');
});
});
describe('slugify', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= slugify() %>', imports)(), '');
});
it('should slugify the characters in a string.', function() {
assert.equal(template('<%= slugify("foo bar baz") %>', imports)(), 'foo-bar-baz');
});
it('should work with hyphens.', function() {
assert.equal(template('<%= slugify("foo-bar-baz") %>', imports)(), 'foo-bar-baz');
assert.equal(template('<%= slugify("-foo bar baz-") %>', imports)(), '-foo-bar-baz-');
});
it('should work with other non-word characters.', function() {
assert.equal(template('<%= slugify("9foo-bar_baz") %>', imports)(), '9foo-bar_baz');
assert.equal(template('<%= slugify("_foo_bar_baz-") %>', imports)(), '_foo_bar_baz-');
});
});
describe('count', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= count() %>', imports)(), '');
});
it('should return zero when the substring is undefined.', function() {
assert.equal(template('<%= count("ababa") %>', imports)(), '0');
});
it('should count the occurrances of a substring.', function() {
assert.equal(template('<%= count("ababa", "a") %>', imports)(), '3');
assert.equal(template('<%= count("abab", "a") %>', imports)(), '2');
assert.equal(template('<%= count("ababaa", "a") %>', imports)(), '4');
});
});
describe('reverse', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= reverse() %>', imports)(), '');
});
it('should reverse the characters in a string.', function() {
assert.equal(template('<%= reverse("abc") %>', imports)(), 'cba');
});
});
describe('wordwrap', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= wordwrap() %>', imports)(), '');
});
it('should wrap words to the specified width.', function() {
var actual = template('<%= wordwrap("a b c d e f", {width: 5}) %>', imports)();
assert.equal(actual, ' a b c \n d e f');
});
it('should use custom newline characters.', function() {
var actual = template('<%= wordwrap("a b c d e f", {width: 5, newline: "<br> "}) %>', imports)();
assert.equal(actual, ' a b c <br> d e f');
});
});
describe('align', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= rightAlign() %>', imports)(), '');
assert.equal(template('<%= centerAlign() %>', imports)(), '');
});
it('should right align the characters in a string.', function() {
var actual = template('<%= rightAlign("foo\\nbarbazb") %>', imports)();
assert.equal(actual, ' foo\nbarbazb');
});
it('should center align the characters in a string.', function() {
var actual = template('<%= centerAlign("foo\\nbarbazb") %>', imports)();
assert.equal(actual, ' foo\nbarbazb');
});
});
describe('replace', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= replace() %>', imports)(), '');
});
it('should return the string when no replacement pattern is passed.', function() {
var actual = template('<%= replace("abcabc") %>', imports)();
assert.equal(actual, 'abcabc');
});
it('should replace characters in a string with nothing.', function() {
var actual = template('<%= replace("abcabc", "a") %>', imports)();
assert.equal(actual, 'bcbc');
});
it('should replace characters in a string with a string', function() {
var actual = template('<%= replace("abcabc", "a", "z") %>', imports)();
assert.equal(actual, 'zbczbc');
});
});
describe('titlecase', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= titlecase("foo") %>', imports)(), 'Foo');
});
it('should upper case the characters in a string.', function() {
var fn = template('<%= titlecase("one two three") %>', imports);
assert.equal(fn(), 'One Two Three');
});
});
describe('truncate', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= truncate() %>', imports)(), '');
});
it('should truncate a string to the specified `length`', function() {
assert.equal(template('<%= truncate("foo bar baz", 7) %>', imports)(), 'foo bar');
assert.equal(template('<%= truncate(sanitize("<span>foo bar baz</span>"), 7) %>', imports)(), 'foo bar');
});
});
describe('ellipsis', function() {
it('should return an empty string when undefined.', function() {
assert.equal(template('<%= ellipsis() %>', imports)(), '');
});
it('should truncate a string to the specified `length` and add an ellipsis.', function() {
assert.equal(template('<%= ellipsis("foo bar baz", 7) %>', imports)(), 'foo bar…');
assert.equal(template('<%= ellipsis(sanitize("<span>foo bar baz</span>"), 7) %>', imports)(), 'foo bar…');
});
});
});