decamelize-keys
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
decamelize | 6.0.0 | 2.69 kB | MIT | prod | |
map-obj | 4.3.0 | 3.12 kB | MIT | prod | |
quick-lru | 6.1.2 | 4.66 kB | MIT | prod | |
type-fest | 3.13.1 | 76.66 kB | (MIT OR CC0-1.0) | prod |
Decamelize-keys is a convenient npm package used to convert object keys from camel case to another style using a separator. This module is designed to transform both individual objects and arrays of objects. This can be particularly useful in scenarios where you need to interact with an API that doesn't follow JavaScript's conventions or if you're dealing with database records and the column names aren't in camel case.
You can easily use decamelize-keys package by first installing it via npm using the command npm install decamelize-keys
.
To convert an object's keys, you can use the decamelizeKeys
function as follows:
import decamelizeKeys from 'decamelize-keys';
// Convert an object
let decamelizedObj = decamelizeKeys({fooBar: true});
console.log(decamelizedObj); // Output: {foo_bar: true}
You can also convert an array of objects:
import decamelizeKeys from 'decamelize-keys';
// Convert an array of objects
let decamelizedArray = decamelizeKeys([{fooBar: true}, {barFoo: false}]);
console.log(decamelizedArray); // Output: [{foo_bar: true}, {bar_foo: false}]
The decamelizeKeys
function also accepts an options object, where you can provide a different separator, exclude certain keys from being decamelized, and even recurse nested objects:
import decamelizeKeys from 'decamelize-keys';
// Use a different separator
let decamelizedObj = decamelizeKeys({fooBar: true}, {separator: '-'});
console.log(decamelizedObj); // Output: {'foo-bar': true}
// Recurse nested objects
let decamelizedObj = decamelizeKeys({fooBar: true, nested: {unicornRainbow: true}}, {deep: true});
console.log(decamelizedObj); // Output: {foo_bar: true, nested: {unicorn_rainbow: true}}
The documents for decamelize-keys npm package can be found on its GitHub repository at https://github.com/sindresorhus/decamelize-keys. The README file there effectively works as the docs, providing installation guidance, code usage examples and available API options.