@jaredwray/fumanchu 2.1.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/string.js DELETED
@@ -1,769 +0,0 @@
1
- 'use strict';
2
-
3
- var isNumber = require('is-number');
4
- var util = require('handlebars-utils');
5
- var utils = require('./utils');
6
- var helpers = module.exports;
7
-
8
- /**
9
- * Append the specified `suffix` to the given string.
10
- *
11
- * ```handlebars
12
- * <!-- given that "item.stem" is "foo" -->
13
- * {{append item.stem ".html"}}
14
- * <!-- results in: 'foo.html' -->
15
- * ```
16
- * @param {String} `str`
17
- * @param {String} `suffix`
18
- * @return {String}
19
- * @api public
20
- */
21
-
22
- helpers.append = function(str, suffix) {
23
- if (typeof str === 'string' && typeof suffix === 'string') {
24
- return str + suffix;
25
- }
26
- return str;
27
- };
28
-
29
- /**
30
- * camelCase the characters in the given `string`.
31
- *
32
- * ```handlebars
33
- * {{camelcase "foo bar baz"}};
34
- * <!-- results in: 'fooBarBaz' -->
35
- * ```
36
- * @param {String} `string` The string to camelcase.
37
- * @return {String}
38
- * @api public
39
- */
40
-
41
- helpers.camelcase = function(str) {
42
- if (!util.isString(str)) return '';
43
- return utils.changecase(str, function(ch) {
44
- return ch.toUpperCase();
45
- });
46
- };
47
-
48
- /**
49
- * Capitalize the first word in a sentence.
50
- *
51
- * ```handlebars
52
- * {{capitalize "foo bar baz"}}
53
- * <!-- results in: "Foo bar baz" -->
54
- * ```
55
- * @param {String} `str`
56
- * @return {String}
57
- * @api public
58
- */
59
-
60
- helpers.capitalize = function(str) {
61
- if (!util.isString(str)) return '';
62
- return str.charAt(0).toUpperCase() + str.slice(1);
63
- };
64
-
65
- /**
66
- * Capitalize all words in a string.
67
- *
68
- * ```handlebars
69
- * {{capitalizeAll "foo bar baz"}}
70
- * <!-- results in: "Foo Bar Baz" -->
71
- * ```
72
- * @param {String} `str`
73
- * @return {String}
74
- * @api public
75
- */
76
-
77
- helpers.capitalizeAll = function(str) {
78
- if (!util.isString(str)) return '';
79
- if (util.isString(str)) {
80
- return str.replace(/\w\S*/g, function(word) {
81
- return helpers.capitalize(word);
82
- });
83
- }
84
- };
85
-
86
- /**
87
- * Center a string using non-breaking spaces
88
- *
89
- * @param {String} `str`
90
- * @param {String} `spaces`
91
- * @return {String}
92
- * @api public
93
- */
94
-
95
- helpers.center = function(str, spaces) {
96
- if (!util.isString(str)) return '';
97
- var space = '';
98
- var i = 0;
99
- while (i < spaces) {
100
- space += '&nbsp;';
101
- i++;
102
- }
103
- return space + str + space;
104
- };
105
-
106
- /**
107
- * Like trim, but removes both extraneous whitespace **and
108
- * non-word characters** from the beginning and end of a string.
109
- *
110
- * ```handlebars
111
- * {{chop "_ABC_"}}
112
- * <!-- results in: 'ABC' -->
113
- *
114
- * {{chop "-ABC-"}}
115
- * <!-- results in: 'ABC' -->
116
- *
117
- * {{chop " ABC "}}
118
- * <!-- results in: 'ABC' -->
119
- * ```
120
- * @param {String} `string` The string to chop.
121
- * @return {String}
122
- * @api public
123
- */
124
-
125
- helpers.chop = function(str) {
126
- if (!util.isString(str)) return '';
127
- return utils.chop(str);
128
- };
129
-
130
- /**
131
- * dash-case the characters in `string`. Replaces non-word
132
- * characters and periods with hyphens.
133
- *
134
- * ```handlebars
135
- * {{dashcase "a-b-c d_e"}}
136
- * <!-- results in: 'a-b-c-d-e' -->
137
- * ```
138
- * @param {String} `string`
139
- * @return {String}
140
- * @api public
141
- */
142
-
143
- helpers.dashcase = function(str) {
144
- if (!util.isString(str)) return '';
145
- return utils.changecase(str, function(ch) {
146
- return '-' + ch;
147
- });
148
- };
149
-
150
- /**
151
- * dot.case the characters in `string`.
152
- *
153
- * ```handlebars
154
- * {{dotcase "a-b-c d_e"}}
155
- * <!-- results in: 'a.b.c.d.e' -->
156
- * ```
157
- * @param {String} `string`
158
- * @return {String}
159
- * @api public
160
- */
161
-
162
- helpers.dotcase = function(str) {
163
- if (!util.isString(str)) return '';
164
- return utils.changecase(str, function(ch) {
165
- return '.' + ch;
166
- });
167
- };
168
-
169
- /**
170
- * Lowercase all of the characters in the given string. Alias for [lowercase](#lowercase).
171
- *
172
- * ```handlebars
173
- * {{downcase "aBcDeF"}}
174
- * <!-- results in: 'abcdef' -->
175
- * ```
176
- * @param {String} `string`
177
- * @return {String}
178
- * @alias lowercase
179
- * @api public
180
- */
181
-
182
- helpers.downcase = function() {
183
- return helpers.lowercase.apply(this, arguments);
184
- };
185
-
186
- /**
187
- * Truncates a string to the specified `length`, and appends
188
- * it with an elipsis, `…`.
189
- *
190
- * ```handlebars
191
- * {{ellipsis (sanitize "<span>foo bar baz</span>"), 7}}
192
- * <!-- results in: 'foo bar…' -->
193
- * {{ellipsis "foo bar baz", 7}}
194
- * <!-- results in: 'foo bar…' -->
195
- * ```
196
- * @param {String} `str`
197
- * @param {Number} `length` The desired length of the returned string.
198
- * @return {String} The truncated string.
199
- * @api public
200
- */
201
-
202
- helpers.ellipsis = function(str, limit) {
203
- if (util.isString(str)) {
204
- if (str.length <= limit) {
205
- return str;
206
- }
207
- return helpers.truncate(str, limit) + '…';
208
- }
209
- };
210
-
211
- /**
212
- * Replace spaces in a string with hyphens.
213
- *
214
- * ```handlebars
215
- * {{hyphenate "foo bar baz qux"}}
216
- * <!-- results in: "foo-bar-baz-qux" -->
217
- * ```
218
- * @param {String} `str`
219
- * @return {String}
220
- * @api public
221
- */
222
-
223
- helpers.hyphenate = function(str) {
224
- if (!util.isString(str)) return '';
225
- return str.split(' ').join('-');
226
- };
227
-
228
- /**
229
- * Return true if `value` is a string.
230
- *
231
- * ```handlebars
232
- * {{isString "foo"}}
233
- * <!-- results in: 'true' -->
234
- * ```
235
- * @param {String} `value`
236
- * @return {Boolean}
237
- * @api public
238
- */
239
-
240
- helpers.isString = function(value) {
241
- return typeof value === 'string';
242
- };
243
-
244
- /**
245
- * Lowercase all characters in the given string.
246
- *
247
- * ```handlebars
248
- * {{lowercase "Foo BAR baZ"}}
249
- * <!-- results in: 'foo bar baz' -->
250
- * ```
251
- * @param {String} `str`
252
- * @return {String}
253
- * @api public
254
- */
255
-
256
- helpers.lowercase = function(str) {
257
- if (util.isObject(str) && str.fn) {
258
- return str.fn(this).toLowerCase();
259
- }
260
- if (!util.isString(str)) return '';
261
- return str.toLowerCase();
262
- };
263
-
264
- /**
265
- * Return the number of occurrences of `substring` within the
266
- * given `string`.
267
- *
268
- * ```handlebars
269
- * {{occurrences "foo bar foo bar baz" "foo"}}
270
- * <!-- results in: 2 -->
271
- * ```
272
- * @param {String} `str`
273
- * @param {String} `substring`
274
- * @return {Number} Number of occurrences
275
- * @api public
276
- */
277
-
278
- helpers.occurrences = function(str, substring) {
279
- if (!util.isString(str)) return '';
280
- var len = substring.length;
281
- var pos = 0;
282
- var n = 0;
283
-
284
- while ((pos = str.indexOf(substring, pos)) > -1) {
285
- n++;
286
- pos += len;
287
- }
288
- return n;
289
- };
290
-
291
- /**
292
- * PascalCase the characters in `string`.
293
- *
294
- * ```handlebars
295
- * {{pascalcase "foo bar baz"}}
296
- * <!-- results in: 'FooBarBaz' -->
297
- * ```
298
- * @param {String} `string`
299
- * @return {String}
300
- * @api public
301
- */
302
-
303
- helpers.pascalcase = function(str) {
304
- if (!util.isString(str)) return '';
305
- str = utils.changecase(str, function(ch) {
306
- return ch.toUpperCase();
307
- });
308
- return str.charAt(0).toUpperCase() + str.slice(1);
309
- };
310
-
311
- /**
312
- * path/case the characters in `string`.
313
- *
314
- * ```handlebars
315
- * {{pathcase "a-b-c d_e"}}
316
- * <!-- results in: 'a/b/c/d/e' -->
317
- * ```
318
- * @param {String} `string`
319
- * @return {String}
320
- * @api public
321
- */
322
-
323
- helpers.pathcase = function(str) {
324
- if (!util.isString(str)) return '';
325
- return utils.changecase(str, function(ch) {
326
- return '/' + ch;
327
- });
328
- };
329
-
330
- /**
331
- * Replace spaces in the given string with pluses.
332
- *
333
- * ```handlebars
334
- * {{plusify "foo bar baz"}}
335
- * <!-- results in: 'foo+bar+baz' -->
336
- * ```
337
- * @param {String} `str` The input string
338
- * @return {String} Input string with spaces replaced by plus signs
339
- * @source Stephen Way <https://github.com/stephenway>
340
- * @api public
341
- */
342
-
343
- helpers.plusify = function(str, ch) {
344
- if (!util.isString(str)) return '';
345
- if (!util.isString(ch)) ch = ' ';
346
- return str.split(ch).join('+');
347
- };
348
-
349
- /**
350
- * Prepends the given `string` with the specified `prefix`.
351
- *
352
- * ```handlebars
353
- * <!-- given that "val" is "bar" -->
354
- * {{prepend val "foo-"}}
355
- * <!-- results in: 'foo-bar' -->
356
- * ```
357
- * @param {String} `str`
358
- * @param {String} `prefix`
359
- * @return {String}
360
- * @api public
361
- */
362
-
363
- helpers.prepend = function(str, prefix) {
364
- return typeof str === 'string' && typeof prefix === 'string'
365
- ? (prefix + str)
366
- : str;
367
- };
368
-
369
- /**
370
- * Render a block without processing mustache templates inside the block.
371
- *
372
- * ```handlebars
373
- * {{{{#raw}}}}
374
- * {{foo}}
375
- * {{{{/raw}}}}
376
- * <!-- results in: '{{foo}}' -->
377
- * ```
378
- *
379
- * @param {Object} `options`
380
- * @return {String}
381
- * @block
382
- * @api public
383
- */
384
-
385
- helpers.raw = function(options) {
386
- var str = options.fn();
387
- var opts = util.options(this, options);
388
- if (opts.escape !== false) {
389
- var idx = 0;
390
- while (((idx = str.indexOf('{{', idx)) !== -1)) {
391
- if (str[idx - 1] !== '\\') {
392
- str = str.slice(0, idx) + '\\' + str.slice(idx);
393
- }
394
- idx += 3;
395
- }
396
- }
397
- return str;
398
- };
399
-
400
- /**
401
- * Remove all occurrences of `substring` from the given `str`.
402
- *
403
- * ```handlebars
404
- * {{remove "a b a b a b" "a "}}
405
- * <!-- results in: 'b b b' -->
406
- * ```
407
- * @param {String} `str`
408
- * @param {String} `substring`
409
- * @return {String}
410
- * @api public
411
- */
412
-
413
- helpers.remove = function(str, ch) {
414
- if (!util.isString(str)) return '';
415
- if (!util.isString(ch)) return str;
416
- return str.split(ch).join('');
417
- };
418
-
419
- /**
420
- * Remove the first occurrence of `substring` from the given `str`.
421
- *
422
- * ```handlebars
423
- * {{remove "a b a b a b" "a"}}
424
- * <!-- results in: ' b a b a b' -->
425
- * ```
426
- * @param {String} `str`
427
- * @param {String} `substring`
428
- * @return {String}
429
- * @api public
430
- */
431
-
432
- helpers.removeFirst = function(str, ch) {
433
- if (!util.isString(str)) return '';
434
- if (!util.isString(ch)) return str;
435
- return str.replace(ch, '');
436
- };
437
-
438
- /**
439
- * Replace all occurrences of substring `a` with substring `b`.
440
- *
441
- * ```handlebars
442
- * {{replace "a b a b a b" "a" "z"}}
443
- * <!-- results in: 'z b z b z b' -->
444
- * ```
445
- * @param {String} `str`
446
- * @param {String} `a`
447
- * @param {String} `b`
448
- * @return {String}
449
- * @api public
450
- */
451
-
452
- helpers.replace = function(str, a, b) {
453
- if (!util.isString(str)) return '';
454
- if (!util.isString(a)) return str;
455
- if (!util.isString(b)) b = '';
456
- return str.split(a).join(b);
457
- };
458
-
459
- /**
460
- * Replace the first occurrence of substring `a` with substring `b`.
461
- *
462
- * ```handlebars
463
- * {{replaceFirst "a b a b a b" "a" "z"}}
464
- * <!-- results in: 'z b a b a b' -->
465
- * ```
466
- * @param {String} `str`
467
- * @param {String} `a`
468
- * @param {String} `b`
469
- * @return {String}
470
- * @api public
471
- */
472
-
473
- helpers.replaceFirst = function(str, a, b) {
474
- if (!util.isString(str)) return '';
475
- if (!util.isString(a)) return str;
476
- if (!util.isString(b)) b = '';
477
- return str.replace(a, b);
478
- };
479
-
480
- /**
481
- * Reverse a string.
482
- *
483
- * ```handlebars
484
- * {{reverse "abcde"}}
485
- * <!-- results in: 'edcba' -->
486
- * ```
487
- * @param {String} `str`
488
- * @return {String}
489
- * @api public
490
- */
491
-
492
- helpers.reverse = function(str) {
493
- if (!util.isString(str)) return '';
494
- return str.split('').reverse().join('');
495
- };
496
-
497
- /**
498
- * Sentence case the given string
499
- *
500
- * ```handlebars
501
- * {{sentence "hello world. goodbye world."}}
502
- * <!-- results in: 'Hello world. Goodbye world.' -->
503
- * ```
504
- * @param {String} `str`
505
- * @return {String}
506
- * @api public
507
- */
508
-
509
- helpers.sentence = function(str) {
510
- if (!util.isString(str)) return '';
511
- return str.replace(/((?:\S[^\.\?\!]*)[\.\?\!]*)/g, function(txt) {
512
- return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
513
- });
514
- };
515
-
516
- /**
517
- * snake_case the characters in the given `string`.
518
- *
519
- * ```handlebars
520
- * {{snakecase "a-b-c d_e"}}
521
- * <!-- results in: 'a_b_c_d_e' -->
522
- * ```
523
- * @param {String} `string`
524
- * @return {String}
525
- * @api public
526
- */
527
-
528
- helpers.snakecase = function(str) {
529
- if (!util.isString(str)) return '';
530
- return utils.changecase(str, function(ch) {
531
- return '_' + ch;
532
- });
533
- };
534
-
535
- /**
536
- * Split `string` by the given `character`.
537
- *
538
- * ```handlebars
539
- * {{split "a,b,c" ","}}
540
- * <!-- results in: ['a', 'b', 'c'] -->
541
- * ```
542
- * @param {String} `string` The string to split.
543
- * @return {String} `character` Default is an empty string.
544
- * @api public
545
- */
546
-
547
- helpers.split = function(str, ch) {
548
- if (!util.isString(str)) return '';
549
- if (!util.isString(ch)) ch = ',';
550
- return str.split(ch);
551
- };
552
-
553
- /**
554
- * Tests whether a string begins with the given prefix.
555
- *
556
- * ```handlebars
557
- * {{#startsWith "Goodbye" "Hello, world!"}}
558
- * Whoops
559
- * {{else}}
560
- * Bro, do you even hello world?
561
- * {{/startsWith}}
562
- * ```
563
- * @contributor Dan Fox <http://github.com/iamdanfox>
564
- * @param {String} `prefix`
565
- * @param {String} `testString`
566
- * @param {String} `options`
567
- * @return {String}
568
- * @block
569
- * @api public
570
- */
571
-
572
- helpers.startsWith = function(prefix, str, options) {
573
- var args = [].slice.call(arguments);
574
- options = args.pop();
575
- if (util.isString(str) && str.indexOf(prefix) === 0) {
576
- return options.fn(this);
577
- }
578
- if (typeof options.inverse === 'function') {
579
- return options.inverse(this);
580
- }
581
- return '';
582
- };
583
-
584
- /**
585
- * Title case the given string.
586
- *
587
- * ```handlebars
588
- * {{titleize "this is title case"}}
589
- * <!-- results in: 'This Is Title Case' -->
590
- * ```
591
- * @param {String} `str`
592
- * @return {String}
593
- * @api public
594
- */
595
-
596
- helpers.titleize = function(str) {
597
- if (!util.isString(str)) return '';
598
- var title = str.replace(/[- _]+/g, ' ');
599
- var words = title.split(' ');
600
- var len = words.length;
601
- var res = [];
602
- var i = 0;
603
- while (len--) {
604
- var word = words[i++];
605
- res.push(exports.capitalize(word));
606
- }
607
- return res.join(' ');
608
- };
609
-
610
- /**
611
- * Removes extraneous whitespace from the beginning and end
612
- * of a string.
613
- *
614
- * ```handlebars
615
- * {{trim " ABC "}}
616
- * <!-- results in: 'ABC' -->
617
- * ```
618
- * @param {String} `string` The string to trim.
619
- * @return {String}
620
- * @api public
621
- */
622
-
623
- helpers.trim = function(str) {
624
- return typeof str === 'string' ? str.trim() : '';
625
- };
626
-
627
- /**
628
- * Removes extraneous whitespace from the beginning of a string.
629
- *
630
- * ```handlebars
631
- * {{trim " ABC "}}
632
- * <!-- results in: 'ABC ' -->
633
- * ```
634
- * @param {String} `string` The string to trim.
635
- * @return {String}
636
- * @api public
637
- */
638
-
639
- helpers.trimLeft = function(str) {
640
- if (util.isString(str)) {
641
- return str.replace(/^\s+/, '');
642
- }
643
- };
644
-
645
- /**
646
- * Removes extraneous whitespace from the end of a string.
647
- *
648
- * ```handlebars
649
- * {{trimRight " ABC "}}
650
- * <!-- results in: ' ABC' -->
651
- * ```
652
- * @param {String} `string` The string to trim.
653
- * @return {String}
654
- * @api public
655
- */
656
-
657
- helpers.trimRight = function(str) {
658
- if (util.isString(str)) {
659
- return str.replace(/\s+$/, '');
660
- }
661
- };
662
-
663
- /**
664
- * Truncate a string to the specified `length`. Also see [ellipsis](#ellipsis).
665
- *
666
- * ```handlebars
667
- * truncate("foo bar baz", 7);
668
- * <!-- results in: 'foo bar' -->
669
- * truncate(sanitize("<span>foo bar baz</span>", 7));
670
- * <!-- results in: 'foo bar' -->
671
- * ```
672
- * @param {String} `str`
673
- * @param {Number} `limit` The desired length of the returned string.
674
- * @param {String} `suffix` Optionally supply a string to use as a suffix to
675
- * denote when the string has been truncated. Otherwise an ellipsis (`…`) will be used.
676
- * @return {String} The truncated string.
677
- * @api public
678
- */
679
-
680
- helpers.truncate = function(str, limit, suffix) {
681
- if (util.isString(str)) {
682
- if (typeof suffix !== 'string') {
683
- suffix = '';
684
- }
685
- if (str.length > limit) {
686
- return str.slice(0, limit - suffix.length) + suffix;
687
- }
688
- return str;
689
- }
690
- };
691
-
692
- /**
693
- * Truncate a string to have the specified number of words.
694
- * Also see [truncate](#truncate).
695
- *
696
- * ```handlebars
697
- * truncateWords("foo bar baz", 1);
698
- * <!-- results in: 'foo...' -->
699
- * truncateWords("foo bar baz", 2);
700
- * <!-- results in: 'foo bar...' -->
701
- * truncateWords("foo bar baz", 3);
702
- * <!-- results in: 'foo bar baz' -->
703
- * ```
704
- * @param {String} `str`
705
- * @param {Number} `limit` The desired length of the returned string.
706
- * @param {String} `suffix` Optionally supply a string to use as a suffix to
707
- * denote when the string has been truncated.
708
- * @return {String} The truncated string.
709
- * @api public
710
- */
711
-
712
- helpers.truncateWords = function(str, count, suffix) {
713
- if (util.isString(str) && isNumber(count)) {
714
- if (typeof suffix !== 'string') {
715
- suffix = '...';
716
- }
717
-
718
- var num = Number(count);
719
- var arr = str.split(' ');
720
- if (num < arr.length) {
721
- arr = arr.slice(0, num);
722
- }
723
-
724
- var val = arr.join(' ').trim();
725
- return val + suffix;
726
- }
727
- };
728
-
729
- /**
730
- * Uppercase all of the characters in the given string. Alias for [uppercase](#uppercase).
731
- *
732
- * ```handlebars
733
- * {{upcase "aBcDeF"}}
734
- * <!-- results in: 'ABCDEF' -->
735
- * ```
736
- * @param {String} `string`
737
- * @return {String}
738
- * @alias uppercase
739
- * @api public
740
- */
741
-
742
- helpers.upcase = function() {
743
- return helpers.uppercase.apply(this, arguments);
744
- };
745
-
746
- /**
747
- * Uppercase all of the characters in the given string. If used as a
748
- * block helper it will uppercase the entire block. This helper
749
- * does not support inverse blocks.
750
- *
751
- * ```handlebars
752
- * {{uppercase "aBcDeF"}}
753
- * <!-- results in: 'ABCDEF' -->
754
- * ```
755
- * @related capitalize capitalizeAll
756
- * @param {String} `str` The string to uppercase
757
- * @param {Object} `options` Handlebars options object
758
- * @return {String}
759
- * @block
760
- * @api public
761
- */
762
-
763
- helpers.uppercase = function(str) {
764
- if (util.isObject(str) && str.fn) {
765
- return str.fn(this).toUpperCase();
766
- }
767
- if (!util.isString(str)) return '';
768
- return str.toUpperCase();
769
- };