UNPKG

2.69 kBMarkdownView Raw
1# camelcase-keys
2
3> Convert object keys to camel case using [`camelcase`](https://github.com/sindresorhus/camelcase)
4
5## Install
6
7```
8$ npm install camelcase-keys
9```
10
11## Usage
12
13```js
14const camelcaseKeys = require('camelcase-keys');
15
16// Convert an object
17camelcaseKeys({'foo-bar': true});
18//=> {fooBar: true}
19
20// Convert an array of objects
21camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]);
22//=> [{fooBar: true}, {barFoo: false}]
23
24camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true});
25//=> {fooBar: true, nested: {unicornRainbow: true}}
26
27camelcaseKeys({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// Convert object keys to pascal case
31camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, pascalCase: true});
32//=> {FooBar: true, Nested: {UnicornRainbow: true}}
33```
34
35```js
36const camelcaseKeys = require('camelcase-keys');
37
38const argv = require('minimist')(process.argv.slice(2));
39//=> {_: [], 'foo-bar': true}
40
41camelcaseKeys(argv);
42//=> {_: [], fooBar: true}
43```
44
45## API
46
47### camelcaseKeys(input, options?)
48
49#### input
50
51Type: `object | object[]`
52
53An object or array of objects to camel-case.
54
55#### options
56
57Type: `object`
58
59##### exclude
60
61Type: `Array<string | RegExp>`\
62Default: `[]`
63
64Exclude keys from being camel-cased.
65
66##### stopPaths
67
68Type: `string[]`\
69Default: `[]`
70
71Exclude 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'`.
72
73```js
74camelcaseKeys({
75 a_b: 1,
76 a_c: {
77 c_d: 1,
78 c_e: {
79 e_f: 1
80 }
81 }
82}, {
83 deep: true,
84 stopPaths: [
85 'a_c.c_e'
86 ]
87}),
88/*
89{
90 aB: 1,
91 aC: {
92 cD: 1,
93 cE: {
94 e_f: 1
95 }
96 }
97}
98*/
99```
100
101##### deep
102
103Type: `boolean`\
104Default: `false`
105
106Recurse nested objects and objects in arrays.
107
108##### pascalCase
109
110Type: `boolean`\
111Default: `false`
112
113Uppercase the first character as in `bye-bye` → `ByeBye`.
114
115## camelcase-keys for enterprise
116
117Available as part of the Tidelift Subscription.
118
119The maintainers of camelcase-keys and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-camelcase-keys?utm_source=npm-camelcase-keys&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
120
121## Related
122
123- [snakecase-keys](https://github.com/bendrucker/snakecase-keys)
124- [kebabcase-keys](https://github.com/mattiloh/kebabcase-keys)
125