decamelcase-keys ·
Convert object keys from camelCase/PascalCase to snake_case or into a lowercased one with a custom separator.
Distinctive features from similar packages:
- convertible object keys are cached to increase performance when re-converting a key
- supports different languages for writing convertible keys (not only English)
This project uses decamelize
and was made on the basis of camelcase-keys
Install
npm i decamelcase-keys
Usage
const decamelcaseKeys = ; // Convert an object;//=> {foo_bar: true} // Convert an array of objects;//=> [{foo_bar: true}, {bar_foo: false}] // Recurse nested objects;//=> {foo_bar: true, nested: {unicorn_rainbow: true}} // Convert object keys with a custom separator (for example, in kebab case);//=> {'foo-bar': true, nested: {'unicorn-rainbow': true}} // Exclude keys from being snake-cased;//=> {foo_bar: true, barFoo: false} // Exclude children at the given object paths;//=> {a_b: 1, a_c: {c_d: 1, c_e: {eF: 1}}}
API
decamelcaseKeys(input, options?)
input
Type: object | object[]
An object or array of objects to snake-case.
options
Type: object
deep
Type: boolean
Default: false
Recurse nested objects and objects in arrays.
separator
Type: string
Default: '_'
The separator to use to put in between the words.
exclude
Type: Array<string | RegExp>
Default: []
Exclude keys from being snake-cased.
stopPaths
Type: string[]
Default: []
Exclude children at the given object paths in dot-notation from being snake-cased. For example, with an object like {a: {b: '🦄'}}
, the object path to reach the unicorn is 'a.b'
.
/*{ a_b: 1, a_c: { c_d: 1, c_e: { eF: 1 } }}*/
Related
See camelcase-keys
for the inverse.
License
decamelcase-keys
is MIT licensed.