-
Notifications
You must be signed in to change notification settings - Fork 1
/
casehelpers.js
149 lines (127 loc) · 3.76 KB
/
casehelpers.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* global chse */
(function () {
// from https://github.com/ianstormtaylor/to-no-case
const hasSpace = /\s/,
hasSeparator = /(_|-|\.|:)/,
hasCamel = /([a-z][A-Z]|[A-Z][a-z])/;
/**
* Remove any starting case from a `string`, like camel or snake, but keep
* spaces and punctuation that may be important otherwise.
*
* @param {String} string
* @return {String}
*/
function toNoCase(string) {
if (hasSpace.test(string)) { return string.toLowerCase(); }
if (hasSeparator.test(string)) { return (unseparate(string) || string).toLowerCase(); }
if (hasCamel.test(string)) { return uncamelize(string).toLowerCase(); }
return string.toLowerCase();
}
/**
* Separator splitter.
*/
const separatorSplitter = /[\W_]+(.|$)/g;
/**
* Un-separate a `string`.
*
* @param {String} string
* @return {String}
*/
function unseparate(string) {
return string.replace(separatorSplitter, (m, next) => (next ? ` ${next}` : ""));
}
/**
* Camelcase splitter.
*/
const camelSplitter = /(.)([A-Z]+)/g;
/**
* Un-camelcase a `string`.
*
* @param {String} string
* @return {String}
*/
function uncamelize(string) {
return string.replace(camelSplitter, (m, previous, uppers) => (
`${previous
} ${
uppers
.toLowerCase()
.split("")
.join(" ")}`
));
}
// via https://github.com/ianstormtaylor/title-case-minors and https://github.com/ianstormtaylor/to-title-case
const minors = [
"a",
"an",
"and",
"as",
"at",
"but",
"by",
"en",
"for",
" from",
"how",
"if",
"in",
"neither",
"nor",
"of",
"on",
"only",
"onto",
"out",
"or",
"per",
"so",
"than",
"that",
"the",
"to",
"until",
"up",
"upon",
"v",
"v.",
"versus",
"vs",
"vs.",
"via",
"when",
"with",
"without",
"yet",
],
escaped = minors.map(str => String(str).replace(/([.*+?=^!:${}()|[\]\/\\])/g, "\\$1")),
minorMatcher = new RegExp(`[^^]\\b(${escaped.join("|")})\\b`, "ig"),
punctuationMatcher = /:\s*(\w)/g;
function toSentenceCase(string) {
return toNoCase(string)
.replace(/[a-z]/i, letter => letter.toUpperCase())
.trim();
}
chse.toTitleCase = function (string) {
return toSentenceCase(string)
.replace(/(^|\s)(\w)/g, (matches, previous, letter) => previous + letter.toUpperCase())
.replace(minorMatcher, minor => minor.toLowerCase())
.replace(punctuationMatcher, letter => letter.toUpperCase());
};
chse.capitalizeFirstLetter = function (word) {
return word.charAt(0) + word.substring(1).toLowerCase();
};
chse.isAllUpcase = function (string) {
for (const ch of string) {
// logs false for up case characters, numbers, symbols
const isLowerCaseCharacter = ch === ch.toLowerCase() && ch !== ch.toUpperCase();
if (isLowerCaseCharacter) { return false; }
}
return true;
};
chse.endsWithPunctuation = function (string) {
const lastCharacter = string.charAt(string.length - 1),
punctuationMarks = [".", "?", "!"];
return punctuationMarks.indexOf(lastCharacter) > -1;
};
}());
chse.debugLog("Case helpers loaded");