-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
71 lines (57 loc) · 1.77 KB
/
utils.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
'use strict';
exports.toWords = input => {
const output = input.replace(/^[_\W]+|[_\W]+$/g, '');
const words = output.split(/[\W_]+/);
return words.length === 1 ? output.split(/(?=[A-Z][^A-Z]*)/) : words.filter(Boolean);
};
exports.changeCase = (input, oneCharFn, transformFn) => {
if (typeof input !== 'string') return '';
const str = input.trim();
if (str === '') return '';
if (str.length === 1) return oneCharFn(str);
return transformFn(str);
};
exports.titlecase = input => {
return exports.changeCase(input, str => str.toLocaleUpperCase(), str => {
const first = str[0].toLocaleUpperCase();
const rest = str.slice(1) || '';
if (rest && !/[a-z]/.test(rest)) {
return first + rest.toLocaleLowerCase();
}
return first + rest;
});
};
exports.titleize = exports.titlecase;
/**
* Convert the given string to pascalcase
* @param {String} input
*/
exports.pascalcase = input => {
return exports.changeCase(input, str => str.toLocaleUpperCase(), str => {
return exports.toWords(str).map(m => exports.titlecase(m)).join('');
});
};
/**
* Convert the given string to camelcase
* @param {String} input
*/
exports.camelcase = input => {
return exports.changeCase(input, str => str.toLocaleLowerCase(), str => {
const output = exports.pascalcase(str);
return output[0].toLocaleLowerCase() + (output.slice(1) || '');
});
};
/**
* Snake-case the given string.
* @param {String} input
*/
exports.snakecase = input => {
return exports.changeCase(input, str => str.toLocaleLowerCase(), str => {
return exports.toWords(str).map(word => word.toLocaleLowerCase()).join('_');
});
};
/**
* Upper-snake-case the given string.
* @param {String} string
*/
exports.uppersnake = input => exports.snakecase(input).toLocaleUpperCase();