-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcasing.ts
142 lines (131 loc) · 2.79 KB
/
casing.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
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
/**
* @fileoverview Casing helpers
* @author Yosuke Ota
*/
/**
* Capitalize a string.
*/
function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
/**
* Checks whether the given string has symbols.
*/
function hasSymbols(str: string) {
return /[!"#%&'()*+,./:;<=>?@[\\\]^`{|}]/u.exec(str) // without " ", "$", "-" and "_"
}
/**
* Checks whether the given string has upper.
*/
function hasUpper(str: string) {
return /[A-Z]/u.exec(str)
}
/**
* Checks whether the given string has lower.
* @param {string} str
*/
function hasLower(str: string) {
return /[a-z]/u.test(str)
}
/**
* Checks whether the given string is kebab-case.
*/
export function isKebabCase(str: string): boolean {
if (
hasUpper(str) ||
hasSymbols(str) ||
/^\d/u.exec(str) ||
/^-/u.exec(str) || // starts with hyphen is not kebab-case
/_|--|\s/u.exec(str)
) {
return false
}
return true
}
/**
* Checks whether the given string is snake_case.
*/
export function isSnakeCase(str: string): boolean {
if (
hasUpper(str) ||
hasSymbols(str) ||
/^\d/u.exec(str) ||
/-|__|\s/u.exec(str)
) {
return false
}
return true
}
/**
* Checks whether the given string is camelCase.
*/
export function isCamelCase(str: string): boolean {
if (
hasSymbols(str) ||
/^[A-Z\d]/u.exec(str) ||
/-|_|\s/u.exec(str) // kebab or snake or space
) {
return false
}
return true
}
/**
* Checks whether the given string is PascalCase.
*/
export function isPascalCase(str: string): boolean {
if (
hasSymbols(str) ||
/^[a-z\d]/u.exec(str) ||
/-|_|\s/u.exec(str) // kebab or snake or space
) {
return false
}
return true
}
/**
* Checks whether the given string is SCREAMING_SNAKE_CASE.
* @param {string} str
*/
export function isScreamingSnakeCase(str: string): boolean {
if (hasLower(str) || hasSymbols(str) || /-|__|\s/u.test(str)) {
return false
}
return true
}
const checkersMap = {
'kebab-case': isKebabCase,
snake_case: isSnakeCase,
camelCase: isCamelCase,
PascalCase: isPascalCase,
SCREAMING_SNAKE_CASE: isScreamingSnakeCase
}
/**
* Convert text to camelCase
*/
export function camelCase(str: string): string {
if (isPascalCase(str)) {
return str.charAt(0).toLowerCase() + str.slice(1)
}
return str.replace(/[-_](\w)/gu, (_, c) => (c ? c.toUpperCase() : ''))
}
/**
* Convert text to PascalCase
*/
export function pascalCase(str: string): string {
return capitalize(camelCase(str))
}
export const allowedCaseOptions = [
'camelCase',
'kebab-case',
'PascalCase',
'snake_case',
'SCREAMING_SNAKE_CASE'
] as const
/**
* Return case checker
*/
export function getCasingChecker(
name: (typeof allowedCaseOptions)[number]
): (str: string) => boolean {
return checkersMap[name] || isPascalCase
}