-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.js
More file actions
101 lines (91 loc) · 1.89 KB
/
format.js
File metadata and controls
101 lines (91 loc) · 1.89 KB
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
/**
* 12345 => 12,345
* @param str
* @return {String}
*/
exports.commify= function(str) {
return String(str)
.split('').reverse().join('')
.replace(/(...)(?!$)/g, '$1,')
.split('').reverse().join('');
};
/**
* Pads a string to minlength by appending spaces.
* @param str
* @param minlength
* @returns {*}
*/
exports.padRight = function (str, minlength) {
while (str.length < minlength) {
str = str + ' ';
}
return str;
};
/**
*
* @param arr
* @returns {*}
*/
exports.longest = function (arr) {
return arr.reduce(function (a, x) {
if (x.length > a) {
return x.length;
}
return a;
}, 0);
};
/**
*
* @param str
* @param max
* @returns {*}
*/
exports.truncate = function (str, max) {
if (str.length <= max) {
return str;
}
return str.substr(0, max - 1) + '…';
};
/**
*
* @param str
* @param suffix
* @returns {boolean}
*/
exports.endsWith = function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
};
var templateRegExp = /\{\{\s*(\w+)\s*\}\}/g;
/**
*
* @param contents
* @param data
* @returns {*}
*/
exports.template = function template(contents, data) {
return contents.toString().replace(templateRegExp, function (match, token) {
var result = data[token];
//Just use empty string for null or undefined
if (result === null || result === undefined) {
result = '';
}
return result;
});
};
/**
*
* @param str
*/
exports.camelize = function(str){
str.trim().replace(/[-_\s]+(.)?/g, function(match, c){
return c.toUpperCase();
})
};
/**
*
* @param str
* @returns {string}
*/
exports.ucfirst = function(str){
return str.charAt(0).toUpperCase() + str.slice(1);
};