-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
106 lines (82 loc) · 2 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* "dot.case"
*/
exports.dotCase = function(string) {
return exports.separatorCase(string, '.');
}
/**
* "ClassCase"
*/
exports.classCase = function(string) {
return exports.separatorCase(string, '_').replace(/(?:^|_|\-|\/)(.)/g, function(match, c) {
return c.toUpperCase();
});
}
/**
* "Namespace.Case"
*/
exports.namespaceCase = function(string) {
return exports.separatorCase(string, '.').replace(/(^|_|\.|\-|\/)(.)/g, function(match, p, c) {
return p + c.toUpperCase();
});
}
/**
* "CONSTANT_CASE"
*/
exports.constantCase = function(string) {
return exports.separatorCase(string, '_').replace(/[a-z]/g, function(c) {
return c.toUpperCase();
});
}
/**
* "camelCase"
*/
exports.camelCase = function(string) {
return exports.separatorCase(string, '_').replace(/[-_\.\/\s]+(.)?/g, function(match, c) {
return c.toUpperCase();
});
}
/**
* "Title Case"
*/
exports.titleCase = function(string) {
return exports.separatorCase(string, ' ').replace(/(?:^|\s)\S/g, function(c) {
return c.toUpperCase();
});
}
/**
* "snake_case"
*/
exports.snakeCase = function(string) {
return exports.separatorCase(string, '_');
}
/**
* "path/case"
*/
exports.pathCase = function(string) {
return this.separatorCase(string, '/');
}
/**
* "param-case"
*/
exports.paramCase = function(string) {
return this.separatorCase(string, '-');
}
/**
* Generic string transform.
*/
exports.separatorCase = function(string, separator) {
return exports.clean(exports.trim(string), separator).replace(/([a-z\d])([A-Z]+)/g, '$1' + separator + '$2').replace(/(([A-Z])(?=[A-Z][a-z]))/g, '$1' + separator).replace(/(([a-z])(?=[A-Z][a-z]))/g, '$1' + separator).replace(/[-\.\/\_\s]+/g, separator).toLowerCase();
}
/**
* Remove non-word characters.
*/
exports.clean = function(string, separator) {
return string.replace(/\W+/g, separator || ' ');
}
/**
* Remove non-word from the start/end of the string only.
*/
exports.trim = function(string) {
return string.replace(/^\W+|\W+$/g, '');
}