Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/camelcase-keys
Convert object keys to camel case
https://github.com/sindresorhus/camelcase-keys
Last synced: 3 days ago
JSON representation
Convert object keys to camel case
- Host: GitHub
- URL: https://github.com/sindresorhus/camelcase-keys
- Owner: sindresorhus
- License: mit
- Created: 2014-10-12T12:14:54.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-06-22T00:32:31.000Z (8 months ago)
- Last Synced: 2025-02-11T18:52:42.167Z (10 days ago)
- Language: TypeScript
- Homepage:
- Size: 85.9 KB
- Stars: 698
- Watchers: 9
- Forks: 95
- Open Issues: 15
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
README
# camelcase-keys
> Convert object keys to camel case using [`camelcase`](https://github.com/sindresorhus/camelcase)
## Install
```sh
npm install camelcase-keys
```## Usage
```js
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}]
``````js
import {parseArgs} from 'node:util';
import camelcaseKeys from 'camelcase-keys';const commandLineArguments = parseArgs();
//=> {_: [], 'foo-bar': true}camelcaseKeys(commandLineArguments);
//=> {_: [], fooBar: true}
```## API
### camelcaseKeys(input, options?)
#### input
Type: `Record | ReadonlyArray>`
A plain object or array of plain objects to camel-case.
#### options
Type: `object`
##### exclude
Type: `Array`\
Default: `[]`Exclude keys from being camel-cased.
##### deep
Type: `boolean`\
Default: `false`Recurse nested objects and objects in arrays.
```js
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}}
```##### pascalCase
Type: `boolean`\
Default: `false`Uppercase the first character: `bye-bye` → `ByeBye`
```js
import camelcaseKeys from 'camelcase-keys';camelcaseKeys({'foo-bar': true}, {pascalCase: true});
//=> {FooBar: true}camelcaseKeys({'foo-bar': true}, {pascalCase: false});
//=> {fooBar: true}
````##### preserveConsecutiveUppercase
Type: `boolean`\
Default: `false`Preserve consecutive uppercase characters: `foo-BAR` → `FooBAR`
```js
import camelcaseKeys from 'camelcase-keys';camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: true});
//=> {fooBAR: true}camelcaseKeys({'foo-BAR': true}, {preserveConsecutiveUppercase: false});
//=> {fooBar: true}
````##### stopPaths
Type: `string[]`\
Default: `[]`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'`.
```js
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
}
}
}
*/
```## Related
- [decamelize-keys](https://github.com/sindresorhus/decamelize-keys) - The inverse of this package
- [snakecase-keys](https://github.com/bendrucker/snakecase-keys)
- [kebabcase-keys](https://github.com/mattiloh/kebabcase-keys)