/
case.ts
253 lines (238 loc) · 5.52 KB
/
case.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
import mapObject from 'map-obj';
import { camelCase } from 'camel-case';
import { pascalCase } from 'pascal-case';
import { snakeCase } from 'snake-case';
import type {
CamelCase,
CamelCasedProperties,
CamelCasedPropertiesDeep,
PascalCase,
PascalCasedProperties,
PascalCasedPropertiesDeep,
SnakeCase,
SnakeCasedProperties,
SnakeCasedPropertiesDeep,
} from 'type-fest';
import type { Options as MapOptions } from 'map-obj';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type PlainObject = Record<string, any>;
function isLastCharNumber(key: string): boolean {
return /^\d$/.test(key[key.length - 1]);
}
function splitLastChar(key: string): string {
return `${key.slice(0, key.length - 1)}_${key.slice(
key.length - 1,
key.length
)}`;
}
/**
* Converts a string to snake case.
*
* @param text - The input string
* @returns The converted string
*
* @example
* ```js
* snakecase('fooBar');
* //=> 'foo_bar'
* ```
*/
function snakecase<T extends string>(text: T): SnakeCase<T> {
const matches = text.match(/\d+/g);
if (!matches) {
return snakeCase(text) as SnakeCase<T>;
}
let modifiedStr: string = text;
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
const mathIndex = modifiedStr.indexOf(match);
modifiedStr = `${modifiedStr.slice(0, mathIndex)}_${modifiedStr.slice(
mathIndex,
modifiedStr.length
)}`;
}
return snakeCase(modifiedStr) as SnakeCase<T>;
}
/**
* Converts object keys to snake case.
*
* @param obj - The input object
* @param options - The options to config this convert function
* @returns The converted object
*
* @example
* ```js
* snakecaseKeys({ 'fooBar': true });
* //=> { 'foo_bar': true }
* ```
*/
function snakecaseKeys<T extends PlainObject, O extends MapOptions>(
obj: T,
options?: O
): O['deep'] extends true
? SnakeCasedPropertiesDeep<T>
: SnakeCasedProperties<T> {
return mapObject(
obj,
(key, val) => [snakecase(key as string), val],
options
) as O['deep'] extends true
? SnakeCasedPropertiesDeep<T>
: SnakeCasedProperties<T>;
}
/**
* Converts object keys to snake case deeply.
*
* @param obj - The input object
* @returns The converted object
*
* @example
* ```js
* snakecaseKeysDeep({ 'fooBar': { 'barFoo': true } });
* //=> { 'foo_bar': { 'bar_foo': true } }
* ```
*/
function snakecaseKeysDeep<T extends PlainObject>(
obj: T
): SnakeCasedPropertiesDeep<T> {
return snakecaseKeys(obj, { deep: true });
}
/**
* Converts a string to camel case.
*
* @param text - The input string
* @returns The converted string
*
* @example
* ```js
* camelcase('foo_bar');
* //=> 'fooBar'
* ```
*/
function camelcase<T extends string>(text: T): CamelCase<T> {
const parts = text.split('_');
const modifiedStr = parts.reduce((acc, part) => {
if (acc === '') return part;
if (/^\d+/.test(part)) {
return acc + part;
}
return `${acc}_${part}`;
}, '');
return camelCase(modifiedStr) as CamelCase<T>;
}
/**
* Converts object keys to camel case.
*
* @param obj - The input object
* @param options - The options to config this convert function
* @returns The converted object
*
* @example
* ```js
* camelcaseKeys({ 'foo_bar': true });
* //=> { 'fooBar': true }
* ```
*/
function camelcaseKeys<T extends PlainObject, O extends MapOptions>(
obj: T,
options?: O
): O['deep'] extends true
? CamelCasedPropertiesDeep<T>
: CamelCasedProperties<T> {
return mapObject(
obj,
(key, val) => [camelcase(key as string), val],
options
) as O['deep'] extends true
? CamelCasedPropertiesDeep<T>
: CamelCasedProperties<T>;
}
/**
* Converts object keys to camel case deeply.
*
* @param obj - The input object
* @returns The converted object
*
* @example
* ```js
* camelcaseKeysDeep({ 'foo_bar': { 'bar_foo': true } });
* //=> { 'fooBar': { 'barFoo': true } }
* ```
*/
function camelcaseKeysDeep<T extends PlainObject>(
obj: T
): CamelCasedPropertiesDeep<T> {
return camelcaseKeys(obj, { deep: true });
}
/**
* Converts a string to pascal case.
*
* @param text - The input string
* @returns The converted string
*
* @example
* ```js
* pascalcase('fooBar');
* //=> 'FooBar'
* ```
*/
function pascalcase<T extends string>(str: T): PascalCase<T> {
return pascalCase(
isLastCharNumber(str) ? splitLastChar(str) : str
) as PascalCase<T>;
}
/**
* Converts object keys to pascal case.
*
* @param obj - The input object
* @param options - The options to config this convert function
* @returns The converted object
*
* @example
* ```js
* pascalcaseKeys({ 'fooBar': true });
* //=> { 'FooBar': true }
* ```
*/
function pascalcaseKeys<T extends PlainObject, O extends MapOptions>(
obj: T,
options?: O
): O['deep'] extends true
? PascalCasedPropertiesDeep<T>
: PascalCasedProperties<T> {
return mapObject(
obj,
(key, val) => [pascalcase(key as string), val],
options
) as O['deep'] extends true
? PascalCasedPropertiesDeep<T>
: PascalCasedProperties<T>;
}
/**
* Converts object keys to pascal case deeply.
*
* @param obj - The input object
* @returns The converted object
*
* @example
* ```js
* pascalcaseKeysDeep({ 'fooBar': { 'barFoo': true } });
* //=> { 'FooBar': { 'BarFoo': true } }
* ```
*/
function pascalcaseKeysDeep<T extends PlainObject>(
obj: T
): PascalCasedPropertiesDeep<T> {
return pascalcaseKeys(obj, { deep: true });
}
export {
snakecase,
snakecaseKeys,
snakecaseKeysDeep,
camelcase,
camelcaseKeys,
camelcaseKeysDeep,
pascalcase,
pascalcaseKeys,
pascalcaseKeysDeep,
};