Skip to content

Commit 46d17e7

Browse files
committed
Improve docs
1 parent 507d5d0 commit 46d17e7

File tree

3 files changed

+132
-74
lines changed

3 files changed

+132
-74
lines changed

index.d.ts

+63-32
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,76 @@ export type CamelCaseKeys<
8787
// Return anything else as-is.
8888
: T;
8989

90-
type Options = {
90+
export type Options = {
91+
/**
92+
Exclude keys from being camel-cased.
93+
94+
If this option can be statically determined, it's recommended to add `as const` to it.
95+
96+
@default []
97+
*/
98+
readonly exclude?: ReadonlyArray<string | RegExp>;
99+
91100
/**
92101
Recurse nested objects and objects in arrays.
93102
94103
@default false
104+
105+
@example
106+
```
107+
import camelcaseKeys from 'camelcase-keys';
108+
109+
const object = {
110+
'foo-bar': true,
111+
nested: {
112+
unicorn_rainbow: true
113+
}
114+
};
115+
116+
camelcaseKeys(object, {deep: true});
117+
//=> {fooBar: true, nested: {unicornRainbow: true}}
118+
119+
camelcaseKeys(object, {deep: false});
120+
//=> {fooBar: true, nested: {unicorn_rainbow: true}}
121+
```
95122
*/
96123
readonly deep?: boolean;
97124

98125
/**
99-
Exclude keys from being camel-cased.
126+
Uppercase the first character: `bye-bye` → `ByeBye`
100127
101-
If this option can be statically determined, it's recommended to add `as const` to it.
128+
@default false
102129
103-
@default []
130+
@example
131+
```
132+
import camelcaseKeys from 'camelcase-keys';
133+
134+
camelcaseKeys({'foo-bar': true}, {pascalCase: true});
135+
//=> {FooBar: true}
136+
137+
camelcaseKeys({'foo-bar': true}, {pascalCase: false});
138+
//=> {fooBar: true}
139+
````
104140
*/
105-
readonly exclude?: ReadonlyArray<string | RegExp>;
141+
readonly pascalCase?: boolean;
142+
143+
/**
144+
Preserve consecutive uppercase characters: `foo-BAR` → `FooBAR`
145+
146+
@default false
147+
148+
@example
149+
```
150+
import camelcaseKeys from 'camelcase-keys';
151+
152+
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: true});
153+
//=> {fooBAR: true}
154+
155+
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: false});
156+
//=> {fooBar: true}
157+
````
158+
*/
159+
readonly preserveConsecutiveUppercase?: boolean;
106160

107161
/**
108162
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'`.
@@ -115,15 +169,17 @@ type Options = {
115169
```
116170
import camelcaseKeys from 'camelcase-keys';
117171
118-
camelcaseKeys({
172+
const object = {
119173
a_b: 1,
120174
a_c: {
121175
c_d: 1,
122176
c_e: {
123177
e_f: 1
124178
}
125179
}
126-
}, {
180+
};
181+
182+
camelcaseKeys(object, {
127183
deep: true,
128184
stopPaths: [
129185
'a_c.c_e'
@@ -141,21 +197,6 @@ type Options = {
141197
```
142198
*/
143199
readonly stopPaths?: readonly string[];
144-
145-
/**
146-
Uppercase the first character as in `bye-bye` → `ByeBye`.
147-
148-
@default false
149-
*/
150-
readonly pascalCase?: boolean;
151-
152-
/**
153-
Preserve consecutive uppercase characters: `foo_BAR` → `FooBAR`.
154-
155-
@default false
156-
*/
157-
readonly preserveConsecutiveUppercase?: boolean;
158-
159200
};
160201

161202
/**
@@ -174,16 +215,6 @@ camelcaseKeys({'foo-bar': true});
174215
// Convert an array of objects
175216
camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);
176217
//=> [{fooBar: true}, {barFoo: false}]
177-
178-
camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true});
179-
//=> {fooBar: true, nested: {unicornRainbow: true}}
180-
181-
camelcaseKeys({a_b: 1, a_c: {c_d: 1, c_e: {e_f: 1}}}, {deep: true, stopPaths: ['a_c.c_e']}),
182-
//=> {aB: 1, aC: {cD: 1, cE: {e_f: 1}}}
183-
184-
// Convert object keys to pascal case
185-
camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, pascalCase: true});
186-
//=> {FooBar: true, Nested: {UnicornRainbow: true}}
187218
```
188219
189220
@example

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656
"camelcase": "^8.0.0",
5757
"map-obj": "5.0.0",
5858
"quick-lru": "^6.1.1",
59-
"type-fest": "^4.2.0"
59+
"type-fest": "^4.3.2"
6060
},
6161
"devDependencies": {
6262
"ava": "^5.3.1",
6363
"matcha": "^0.7.0",
64-
"tsd": "^0.28.1",
64+
"tsd": "^0.29.0",
6565
"xo": "^0.56.0"
6666
},
6767
"xo": {

readme.md

+67-40
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ camelcaseKeys({'foo-bar': true});
2020
// Convert an array of objects
2121
camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);
2222
//=> [{fooBar: true}, {barFoo: false}]
23-
24-
camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true});
25-
//=> {fooBar: true, nested: {unicornRainbow: true}}
26-
27-
camelcaseKeys({a_b: 1, a_c: {c_d: 1, c_e: {e_f: 1}}}, {deep: true, stopPaths: ['a_c.c_e']}),
28-
//=> {aB: 1, aC: {cD: 1, cE: {e_f: 1}}}
29-
30-
// preserve Uppercase if they are consecutive
31-
camelcaseKeys({'foo-BAR': true, nested: {unicorn_RAINbow: true}}, {deep: true, preserveConsecutiveUppercase: false});
32-
//=> {fooBar: true, nested: {unicornRainbow: true}}
33-
camelcaseKeys({'foo-BAR': true, nested: {unicorn_RAINbow: true}}, {deep: true, preserveConsecutiveUppercase: true});
34-
//=> {fooBAR: true, nested: {unicornRAINbow: true}}
35-
36-
// Convert object keys to pascal case
37-
camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, pascalCase: true});
38-
//=> {FooBar: true, Nested: {UnicornRainbow: true}}
3923
```
4024

4125
```js
@@ -70,23 +54,87 @@ Default: `[]`
7054

7155
Exclude keys from being camel-cased.
7256

57+
##### deep
58+
59+
Type: `boolean`\
60+
Default: `false`
61+
62+
Recurse nested objects and objects in arrays.
63+
64+
```js
65+
import camelcaseKeys from 'camelcase-keys';
66+
67+
const object = {
68+
'foo-bar': true,
69+
nested: {
70+
unicorn_rainbow: true
71+
}
72+
};
73+
74+
camelcaseKeys(object, {deep: true});
75+
//=> {fooBar: true, nested: {unicornRainbow: true}}
76+
77+
camelcaseKeys(object, {deep: false});
78+
//=> {fooBar: true, nested: {unicorn_rainbow: true}}
79+
```
80+
81+
##### pascalCase
82+
83+
Type: `boolean`\
84+
Default: `false`
85+
86+
Uppercase the first character: `bye-bye``ByeBye`
87+
88+
```js
89+
import camelcaseKeys from 'camelcase-keys';
90+
91+
camelcaseKeys({'foo-bar': true}, {pascalCase: true});
92+
//=> {FooBar: true}
93+
94+
camelcaseKeys({'foo-bar': true}, {pascalCase: false});
95+
//=> {fooBar: true}
96+
````
97+
98+
##### preserveConsecutiveUppercase
99+
100+
Type: `boolean`\
101+
Default: `false`
102+
103+
Preserve consecutive uppercase characters: `foo-BAR``FooBAR`
104+
105+
```js
106+
import camelcaseKeys from 'camelcase-keys';
107+
108+
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: true});
109+
//=> {fooBAR: true}
110+
111+
camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: false});
112+
//=> {fooBar: true}
113+
````
114+
73115
##### stopPaths
74116
75117
Type: `string[]`\
76118
Default: `[]`
77119
78-
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'`.
120+
Exclude children at the given object paths in dot-notation from being camel-cased.
121+
122+
For example, with an object like `{a: {b: '🦄'}}`, the object path to reach the unicorn is `'a.b'`.
79123
80124
```js
81-
camelcaseKeys({
125+
import camelcaseKeys from 'camelcase-keys';
126+
127+
const object = {
82128
a_b: 1,
83129
a_c: {
84130
c_d: 1,
85131
c_e: {
86132
e_f: 1
87133
}
88134
}
89-
}, {
135+
};
136+
137+
camelcaseKeys(object, {
90138
deep: true,
91139
stopPaths: [
92140
'a_c.c_e'
@@ -105,27 +153,6 @@ camelcaseKeys({
105153
*/
106154
```
107155

108-
##### deep
109-
110-
Type: `boolean`\
111-
Default: `false`
112-
113-
Recurse nested objects and objects in arrays.
114-
115-
##### pascalCase
116-
117-
Type: `boolean`\
118-
Default: `false`
119-
120-
Uppercase the first character as in `bye-bye``ByeBye`.
121-
122-
##### preserveConsecutiveUppercase
123-
124-
Type: `boolean`\
125-
Default: `false`
126-
127-
Preserve consecutive uppercase characters: `foo-BAR``FooBAR` if true vs `foo-BAR``FooBar` if false
128-
129156
## Related
130157

131158
- [decamelize-keys](https://github.com/sindresorhus/decamelize-keys) - The inverse of this package

0 commit comments

Comments
 (0)