forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (75 loc) · 1.71 KB
/
index.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
'use strict';
var util = require('handlebars-utils');
var utils = require('./utils');
/**
* Returns true if the given value contains the given
* `object`, optionally passing a starting index.
*
* @param {Array} val
* @param {Object} obj
* @param {Number} start
* @return {Boolean}
*/
utils.contains = function(val, obj, start) {
if (val == null || obj == null || !utils.isNumber(val.length)) {
return false;
}
return val.indexOf(obj, start) !== -1;
};
/**
* Remove leading and trailing whitespace and non-word
* characters from the given string.
*
* @param {String} `str`
* @return {String}
*/
utils.chop = function(str) {
if (!util.isString(str)) return '';
var re = /^[-_.\W\s]+|[-_.\W\s]+$/g;
return str.trim().replace(re, '');
};
/**
* Change casing on the given `string`, optionally
* passing a delimiter to use between words in the
* returned string.
*
* ```handlebars
* utils.changecase('fooBarBaz');
* //=> 'foo bar baz'
*
* utils.changecase('fooBarBaz' '-');
* //=> 'foo-bar-baz'
* ```
* @param {String} `string` The string to change.
* @return {String}
* @api public
*/
utils.changecase = function(str, fn) {
if (!util.isString(str)) return '';
if (str.length === 1) {
return str.toLowerCase();
}
str = utils.chop(str).toLowerCase();
if (typeof fn !== 'function') {
fn = utils.identity;
}
var re = /[-_.\W\s]+(\w|$)/g;
return str.replace(re, function(_, ch) {
return fn(ch);
});
};
/**
* Generate a random number
*
* @param {Number} `min`
* @param {Number} `max`
* @return {Number}
* @api public
*/
utils.random = function(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
};
/**
* Expose `utils`
*/
module.exports = utils;