-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.js
More file actions
93 lines (72 loc) · 1.96 KB
/
format.js
File metadata and controls
93 lines (72 loc) · 1.96 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
/**
* 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 {String} str
* @param {Number} minlength
* @return {String}
* @api public
*/
exports.padRight = function (str, minlength) {
while (str.length < minlength) {
str = str + ' ';
}
return str;
};
exports.longest = function (arr) {
return arr.reduce(function (a, x) {
if (x.length > a) {
return x.length;
}
return a;
}, 0);
};
exports.ISODateString = function (d) {
function pad(n){
return n < 10 ? '0' + n : n;
}
return d.getUTCFullYear() + '-' +
pad(d.getUTCMonth() + 1) + '-' +
pad(d.getUTCDate()) + 'T' +
pad(d.getUTCHours()) + ':' +
pad(d.getUTCMinutes()) + ':' +
pad(d.getUTCSeconds()) + 'Z';
};
exports.truncate = function (str, max) {
if (str.length <= max) {
return str;
}
return str.substr(0, max - 1) + '…';
};
exports.endsWith = function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
};
var tokenRegExp = /\{\{(\w+)\}\}/g;
exports.template = function template(contents, data) {
return contents.replace(tokenRegExp, function (match, token) {
var result = data[token];
//Just use empty string for null or undefined
if (result === null || result === undefined) {
result = '';
}
return result;
});
};
exports.camelize = function(str){
str.trim().replace(/[-_\s]+(.)?/g, function(match, c){
return c.toUpperCase();
})
};
exports.ucfirst = function(str){
return str.charAt(0).toUpperCase() + str.slice(1);
};