-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
index.d.ts
246 lines (206 loc) · 5.8 KB
/
index.d.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
import type {CamelCase, PascalCase} from 'type-fest';
// eslint-disable-next-line @typescript-eslint/ban-types
type EmptyTuple = [];
// Allow union with, for example, `undefined` and `null`.
type ObjectUnion = Record<string, unknown> | unknown;
/**
Return a default type if input type is nil.
@template T - Input type.
@template U - Default type.
*/
type WithDefault<T, U> = T extends undefined | void | null ? U : T; // eslint-disable-line @typescript-eslint/ban-types
// TODO: Replace this with https://github.com/sindresorhus/type-fest/blob/main/source/includes.d.ts
/**
Check if an element is included in a tuple.
*/
type IsInclude<List extends readonly unknown[], Target> = List extends undefined
? false
: List extends Readonly<EmptyTuple>
? false
: List extends readonly [infer First, ...infer Rest]
? First extends Target
? true
: IsInclude<Rest, Target>
: boolean;
/**
Append a segment to dot-notation path.
*/
type AppendPath<S extends string, Last extends string> = S extends ''
? Last
: `${S}.${Last}`;
/**
Convert keys of an object to camelcase strings.
*/
export type CamelCaseKeys<
T extends ObjectUnion | ReadonlyArray<Record<string, unknown>>,
Deep extends boolean = false,
IsPascalCase extends boolean = false,
PreserveConsecutiveUppercase extends boolean = false,
Exclude extends readonly unknown[] = EmptyTuple,
StopPaths extends readonly string[] = EmptyTuple,
Path extends string = '',
> = T extends ReadonlyArray<Record<string, unknown>>
// Handle arrays or tuples.
? {
[P in keyof T]: T[P] extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>
? CamelCaseKeys<
T[P],
Deep,
IsPascalCase,
PreserveConsecutiveUppercase,
Exclude,
StopPaths
>
: T[P];
}
: T extends Record<string, unknown>
// Handle objects.
? {
[P in keyof T as [IsInclude<Exclude, P>] extends [true]
? P
: [IsPascalCase] extends [true]
? PascalCase<P>
: CamelCase<P, {preserveConsecutiveUppercase: PreserveConsecutiveUppercase}>]: [IsInclude<StopPaths, AppendPath<Path, P & string>>] extends [
true,
]
? T[P]
: [Deep] extends [true]
? T[P] extends ObjectUnion | ReadonlyArray<Record<string, unknown>>
? CamelCaseKeys<
T[P],
Deep,
IsPascalCase,
PreserveConsecutiveUppercase,
Exclude,
StopPaths,
AppendPath<Path, P & string>
>
: T[P]
: T[P];
}
// Return anything else as-is.
: T;
export type Options = {
/**
Exclude keys from being camel-cased.
If this option can be statically determined, it's recommended to add `as const` to it.
@default []
*/
readonly exclude?: ReadonlyArray<string | RegExp>;
/**
Recurse nested objects and objects in arrays.
@default false
@example
```
import camelcaseKeys from 'camelcase-keys';
const object = {
'foo-bar': true,
nested: {
unicorn_rainbow: true
}
};
camelcaseKeys(object, {deep: true});
//=> {fooBar: true, nested: {unicornRainbow: true}}
camelcaseKeys(object, {deep: false});
//=> {fooBar: true, nested: {unicorn_rainbow: true}}
```
*/
readonly deep?: boolean;
/**
Uppercase the first character: `bye-bye` → `ByeBye`
@default false
@example
```
import camelcaseKeys from 'camelcase-keys';
camelcaseKeys({'foo-bar': true}, {pascalCase: true});
//=> {FooBar: true}
camelcaseKeys({'foo-bar': true}, {pascalCase: false});
//=> {fooBar: true}
````
*/
readonly pascalCase?: boolean;
/**
Preserve consecutive uppercase characters: `foo-BAR` → `FooBAR`
@default false
@example
```
import camelcaseKeys from 'camelcase-keys';
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: true});
//=> {fooBAR: true}
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: false});
//=> {fooBar: true}
````
*/
readonly preserveConsecutiveUppercase?: boolean;
/**
Exclude children at the given object paths in dot-notation from being camel-cased. For example, with an object like `{a: {b: '🦄'}}`, the object path to reach the unicorn is `'a.b'`.
If this option can be statically determined, it's recommended to add `as const` to it.
@default []
@example
```
import camelcaseKeys from 'camelcase-keys';
const object = {
a_b: 1,
a_c: {
c_d: 1,
c_e: {
e_f: 1
}
}
};
camelcaseKeys(object, {
deep: true,
stopPaths: [
'a_c.c_e'
]
}),
// {
// aB: 1,
// aC: {
// cD: 1,
// cE: {
// e_f: 1
// }
// }
// }
```
*/
readonly stopPaths?: readonly string[];
};
/**
Convert object keys to camel case using [`camelcase`](https://github.com/sindresorhus/camelcase).
@param input - Object or array of objects to camel-case.
@example
```
import camelcaseKeys from 'camelcase-keys';
// Convert an object
camelcaseKeys({'foo-bar': true});
//=> {fooBar: true}
// Convert an array of objects
camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);
//=> [{fooBar: true}, {barFoo: false}]
```
@example
```
import {parseArgs} from 'node:util';
import camelcaseKeys from 'camelcase-keys';
const commandLineArguments = parseArgs();
//=> {_: [], 'foo-bar': true}
camelcaseKeys(commandLineArguments);
//=> {_: [], fooBar: true}
```
*/
export default function camelcaseKeys<
T extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>,
OptionsType extends Options = Options,
>(
input: T,
options?: OptionsType
): CamelCaseKeys<
T,
WithDefault<'deep' extends keyof OptionsType ? OptionsType['deep'] : undefined, false>,
WithDefault<'pascalCase' extends keyof OptionsType ? OptionsType['pascalCase'] : undefined, false>,
WithDefault<'preserveConsecutiveUppercase' extends keyof OptionsType ? OptionsType['preserveConsecutiveUppercase'] : undefined, false>,
WithDefault<'exclude' extends keyof OptionsType ? OptionsType['exclude'] : undefined, EmptyTuple>,
WithDefault<'stopPaths' extends keyof OptionsType ? OptionsType['stopPaths'] : undefined, EmptyTuple>
>;