-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.ts
257 lines (236 loc) · 6.51 KB
/
tools.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { NamingConvention } from './modeler';
/**
* `Convention` is an object with a `to` function that takes a string and returns a string, and an `is`
* function that takes a string and returns a boolean.
*
* @property to - A function that takes a string and returns a string.
* @property is - A function that takes a string and returns a boolean.
*/
export type Convention = {
to: (str: string) => string;
is: (str: string) => boolean;
};
/**
* It takes a naming convention and returns an object with two functions: `to` and `is`
*
* @param {NamingConvention} name - NamingConvention
*
* @returns An object with two properties: to and is.
*/
export function convention(name: NamingConvention): Convention {
let to: (str: string) => string;
let is: (str: string) => boolean;
switch (name) {
case 'snake_case':
to = toSnakeCase;
is = isSnakeCase;
break;
case 'camelCase':
to = toCamelCase;
is = isCamelCase;
break;
case 'PascalCase':
to = toPascalCase;
is = isPascalCase;
break;
case 'kebab-case':
to = toKebabCase;
is = isKebabCase;
break;
case 'MACRO_CASE':
to = toMacroCase;
is = isMacroCase;
break;
case 'Train-Case':
to = toTrainCase;
is = isTrainCase;
break;
case 'flatcase':
to = toFlatCase;
is = isFlatCase;
break;
case 'no case':
to = toNoCase;
is = isNoCase;
break;
default:
throw new Error('Naming convention is not defined');
}
return { to, is };
}
/**
* It replaces all capital letters with a space and the capital letter, replaces all capital letter
* sequences with a space and the capital letter sequence, replaces all spaces, underscores, and
* hyphens with a space, converts the string to lowercase, and trims the string
*
* @param {string} str - The string to convert.
*
* @returns A string with no case.
*/
export function toNoCase(str: string): string {
return str
.replace(/[A-Z][a-z]+/g, (m) => ' ' + m)
.replace(/[A-Z][A-Z]+/g, (m) => ' ' + m)
.replace(/[\s-_]+/g, ' ')
.trim();
}
/**
* If the string is already in no case, return true, otherwise return false.
*
* @param {string} str - The string to check.
*
* @returns A boolean value.
*/
export function isNoCase(str: string): boolean {
return toNoCase(str) === str;
}
/**
* It converts a string to snake case by converting it to no case and replacing all spaces with
* underscores
*
* @param {string} str - The string to convert.
*
* @returns A function that takes a string and returns a string with all the spaces replaced with
* underscores.
*/
export function toSnakeCase(str: string): string {
return toNoCase(str).toLowerCase().replace(/\s+/g, '_');
}
/**
* If the string is already in snake case, then return true.
*
* @param {string} str - The string to check.
*
* @returns A boolean value.
*/
export function isSnakeCase(str: string): boolean {
return toSnakeCase(str) === str;
}
/**
* Replace all non-word characters with a space, then replace all spaces with nothing, then replace all
* lowercase letters that follow a non-word character with an uppercase letter, then replace all spaces
* with nothing.
*
* @param {string} str - The string to convert.
*
* @returns A string with the first letter capitalized.
*/
export function toCamelCase(str: string): string {
return toNoCase(str)
.toLowerCase()
.replace(/[^\w][a-z]/g, (m) => m.toUpperCase())
.replace(/\s+/g, '');
}
/**
* If the string is already camelCase, then return true, otherwise return false.
*
* @param {string} str - The string to check.
*
* @returns A boolean value.
*/
export function isCamelCase(str: string): boolean {
return toCamelCase(str) === str;
}
/**
* Convert a string to pascal case.
*
* @param {string} str - The string to convert to PascalCase.
*
* @returns A string with the first letter capitalized.
*/
export function toPascalCase(str: string): string {
return (' ' + toNoCase(str)).replace(/[^\w][a-z]/g, (m) => m.toUpperCase()).replace(/\s+/g, '');
}
/**
* It returns true if the string is already in PascalCase
*
* @param {string} str - The string to check.
*
* @returns A boolean value.
*/
export function isPascalCase(str: string): boolean {
return toPascalCase(str) === str;
}
/**
* It converts a string to a macro case string
*
* @param {string} str - The string to convert.
*
* @returns A string with all lowercase letters and spaces replaced with underscores.
*/
export function toMacroCase(str: string): string {
return toNoCase(str).toLowerCase().replace(/\s+/g, '_').toUpperCase();
}
/**
* Convert a string to MacroCase, and return true if the result is the same as the input.
*
* @param {string} str - The string to convert to MacroCase.
*
* @returns A boolean value.
*/
export function isMacroCase(str: string): boolean {
return toMacroCase(str) === str;
}
/**
* It takes a string, converts it to no case, and replaces all spaces with dashes
*
* @param {string} str - The string to convert.
*
* @returns A function that takes a string and returns a string.
*/
export function toKebabCase(str: string): string {
return toNoCase(str).toLowerCase().replace(/\s+/g, '-');
}
/**
* It returns true if the input string is already in kebab case
*
* @param {string} str - The string to check.
*
* @returns true
*/
export function isKebabCase(str: string): boolean {
return toKebabCase(str) === str;
}
/**
* It takes a string, converts it to train case
*
* @param {string} str - The string to convert.
*
* @returns A function that takes a string and returns a string.
*/
export function toTrainCase(str: string): string {
return (' ' + toNoCase(str))
.replace(/[^\w][a-z]/g, (m) => m.toUpperCase())
.trim()
.replace(/\s+/g, '-');
}
/**
* It returns true if the input string is already in train case
*
* @param {string} str - The string to check.
*
* @returns true
*/
export function isTrainCase(str: string): boolean {
return toTrainCase(str) === str;
}
/**
* It takes a string, converts it to flat case, and replaces all spaces with nothing
*
* @param {string} str - The string to convert.
*
* @returns A function that takes a string and returns a string.
*/
export function toFlatCase(str: string): string {
return toNoCase(str).toLowerCase().replace(/\s+/g, '');
}
/**
* It returns true if the input string is already in flat case
*
* @param {string} str - The string to check.
*
* @returns true
*/
export function isFlatCase(str: string): boolean {
return toFlatCase(str) === str;
}