-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathutilities.ts
45 lines (37 loc) · 1.04 KB
/
utilities.ts
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
// Checks whether character is Uppercase.
// Crude version. Checks only A-Z.
function isCaps(char: string) {
if (char.match(/[A-Z]/)) return true;
return false;
}
// Checks whether character is digit.
function isDigit(char: string) {
if (char.match(/[0-9]/)) return true;
return false;
}
export function toKebab(string: string) {
return string
.split('')
.map((letter, index) => {
const previousLetter = string[index - 1] || '';
const currentLetter = letter;
if (isDigit(currentLetter) && !isDigit(previousLetter)) {
return `-${currentLetter}`;
}
if (!isCaps(currentLetter)) return currentLetter;
if (previousLetter === '') {
return `${currentLetter.toLowerCase()}`;
}
if (isCaps(previousLetter)) {
return `${currentLetter.toLowerCase()}`;
}
return `-${currentLetter.toLowerCase()}`;
})
.join('')
.trim()
.replace(/[-_\s]+/g, '-');
}
export function toSentence(string: string) {
const interim = toKebab(string).replace(/-/g, ' ');
return interim.slice(0, 1).toUpperCase() + interim.slice(1);
}