-
Notifications
You must be signed in to change notification settings - Fork 33
/
util.js
44 lines (37 loc) · 866 Bytes
/
util.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
/**
* @file util
* @author zhangwentao
*/
/**
* Create a cached version of a pure function.
*/
function cached(fn) {
const cache = Object.create(null);
return function cachedFn(str) {
const hit = cache[str];
return hit || (cache[str] = fn(str));
};
}
/**
* Camelize a hyphen-delimited string.
*/
const camelizeRE = /-(\w)/g;
const camelize = cached(str => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '');
});
/**
* Capitalize a string.
*/
const capitalize = cached(str => {
return str.charAt(0).toUpperCase() + str.slice(1);
});
/**
* Hyphenate a camelCase string.
*/
const hyphenateRE = /\B([A-Z])/g;
const hyphenate = cached(str => {
return str.replace(hyphenateRE, '-$1').toLowerCase();
});
exports.camelize = camelize;
exports.capitalize = capitalize;
exports.hyphenate = hyphenate;