-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.ts
246 lines (227 loc) · 7.61 KB
/
string.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
/**
* Capitalizes the first letter of a string
* @param str string to capitalize
* @returns the string with the first letter capitalized
* @example
* capitalize('foo') // returns 'Foo'
* capitalize('FOO') // returns 'FOO'
* capitalize('123') // returns '123'
* capitalize('') // returns ''
*/
export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
/**
* Converts a string to camelCase
* @param str string to convert
* @returns the string in camelCase
* @example
* camelCase('foo-bar') // returns 'fooBar'
* camelCase('foo_bar') // returns 'fooBar'
* camelCase('FooBar') // returns 'fooBar'
* camelCase('FOO_BAR') // returns 'fooBar'
*/
export function camelCase(str: string): string {
const words = separateWords(str);
return words
.map((word, index) =>
index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
)
.join('');
}
/**
* Converts a string to kebab-case
* @param str string to convert
* @param options options for the conversion
* @returns the string in kebab-case
* @example
* kebabCase('fooBar') // returns 'foo-bar'
* kebabCase('foo_bar') // returns 'foo-bar'
* kebabCase('foo-bar') // returns 'foo-bar'
* kebabCase('FOO_BAR') // returns 'foo-bar'
* kebabCase('foo') // returns 'foo'
* kebabCase('') // returns ''
* kebabCase('fooBar', { screaming: true }) // returns 'FOO-BAR'
* kebabCase('foo_bar', { screaming: true }) // returns 'FOO-BAR'
* kebabCase('foo-bar', { screaming: true }) // returns 'FOO-BAR'
* kebabCase('FOO_BAR', { screaming: true }) // returns 'FOO-BAR'
* kebabCase('foo', { screaming: true }) // returns 'FOO'
*/
export function kebabCase(str: string, options = { screaming: false }) {
const words = separateWords(str).map(word => word.toLowerCase());
const kebab = words.join('-');
return options.screaming ? kebab.toUpperCase() : kebab;
}
/**
* Converts a string to snake_case
* @param str string to convert
* @param options options for the conversion
* @returns the string in snake_case
* @example
* snakeCase('fooBar') // returns 'foo_bar'
* snakeCase('foo-bar') // returns 'foo_bar'
* snakeCase('foo_bar') // returns 'foo_bar'
* snakeCase('FOO_BAR') // returns 'foo_bar'
* snakeCase('foo') // returns 'foo'
* snakeCase('') // returns ''
* snakeCase('fooBar', { screaming: true }) // returns 'FOO_BAR'
* snakeCase('foo-bar', { screaming: true }) // returns 'FOO_BAR'
* snakeCase('foo_bar', { screaming: true }) // returns 'FOO_BAR'
* snakeCase('FOO_BAR', { screaming: true }) // returns 'FOO_BAR'
* snakeCase('foo', { screaming: true }) // returns 'FOO'
* snakeCase('fooBar', { screaming: true }) // returns 'FOO_BAR'
* snakeCase('foo-bar', { screaming: true }) // returns 'FOO_BAR'
*/
export function snakeCase(str: string, options = { screaming: false }) {
const words = separateWords(str).map(word => word.toLowerCase());
const snake = words.join('_');
return options.screaming ? snake.toUpperCase() : snake;
}
/**
* Converts a string to PascalCase
* @param str string to convert
* @returns the string in PascalCase
* @example
* pascalCase('fooBar') // returns 'FooBar'
* pascalCase('foo-bar') // returns 'FooBar'
* pascalCase('foo_bar') // returns 'FooBar'
* pascalCase('FOO_BAR') // returns 'FooBar'
* pascalCase('foo') // returns 'Foo'
* pascalCase('') // returns ''
*/
export function pascalCase(str: string): string {
const words = separateWords(str);
return words
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join('');
}
/**
* Converts a string to lower case
* @param str string to convert
* @returns the string in lower case
* @example
* lowerCase('FOO') // returns 'foo'
* lowerCase('foo') // returns 'foo'
* lowerCase('123') // returns '123'
* lowerCase('') // returns ''
*/
export function lowerCase(str: string): string {
return str.toLowerCase();
}
/**
* Converts a string to upper case
* @param str string to convert
* @returns the string in upper case
* @example
* upperCase('foo') // returns 'FOO'
* upperCase('FOO') // returns 'FOO'
* upperCase('123') // returns '123'
* upperCase('') // returns ''
* upperCase('foo-bar') // returns 'FOO-BAR'
*/
export function upperCase(str: string): string {
return str.toUpperCase();
}
/**
* Detects the case of a string
* @param str string to detect the case of
* @returns the detected case of the string
* @example
* detectCase('fooBar') // returns 'camel'
* detectCase('foo-bar') // returns 'kebab'
* detectCase('FOO-BAR') // returns 'screaming-kebab'
* detectCase('foo_bar') // returns 'snake'
* detectCase('FOO_BAR') // returns 'screaming-snake'
* detectCase('FooBar') // returns 'pascal'
* detectCase('FOO BAR') // returns 'upper'
* detectCase('foo bar') // returns 'lower'
* detectCase('fooBar-baz') // returns 'mixed
* detectCase('foo BAR') // returns 'mixed
* detectCase('') // returns 'none'
*/
export function detectCase(str: string): 'camel' | 'kebab' | 'screaming-kebab' | 'snake' | 'screaming-snake' | 'pascal' | 'lower' | 'upper' | 'mixed' | 'none' {
if (!str) {
return 'none';
}
const hasDash = str.includes('-');
const hasUnderscore = str.includes('_');
const hasSpace = str.includes(' ');
const noSeparator = !hasDash && !hasUnderscore && !hasSpace;
if ((hasDash && (hasUnderscore || hasSpace)) || (hasUnderscore && hasSpace)) {
return 'mixed';
}
if (hasDash) {
if (str === str.toUpperCase()) {
return 'screaming-kebab';
}
if (str === str.toLowerCase()) {
return 'kebab';
}
}
if (hasUnderscore) {
if (str === str.toUpperCase()) {
return 'screaming-snake';
}
if (str === str.toLowerCase()) {
return 'snake';
}
}
if (str === str.toUpperCase()) {
return 'upper';
}
if (str === str.toLowerCase()) {
return 'lower';
}
if (noSeparator) {
if (str[0] === str[0].toUpperCase()) {
return 'pascal';
}
return 'camel';
}
return 'mixed';
}
/**
* Separates words in a string based on the detected case.
* @param str The string to separate.
* @returns An array of separated words.
* @example
* separateWords('fooBar') // returns ['foo', 'Bar']
* separateWords('foo-bar') // returns ['foo', 'bar']
* separateWords('FOO_BAR') // returns ['FOO', 'BAR']
* separateWords('foo bar') // returns ['foo', 'bar']
* separateWords('') // returns []
* separateWords('fooBar-baz') // returns ['foo', 'Bar', 'baz']
*/
export function separateWords(str: string): string[] {
const caseType = detectCase(str);
switch (caseType) {
case 'camel':
case 'pascal':
return str.replace(/([a-z])([A-Z])/g, '$1 $2').split(' ');
case 'snake':
case 'screaming-snake':
return str.split('_');
case 'kebab':
case 'screaming-kebab':
return str.split('-');
case 'lower':
case 'upper':
return str.split(' ');
case 'mixed':
throw new Error('Cannot separate words in a mixed case string');
case 'none':
return [];
default:
throw new Error('Unknown case type');
}
}
/**
* Splits a string into an array of substrings based on a separator
* @param str string to split
* @param separator separator to split the string by
* @param limit maximum number of substrings to return
* @returns an array of substrings
*/
export function split(str: string, separator: string | RegExp, limit?: number): string[] {
return str.split(separator, limit);
}