What is snakecase-keys?
The snakecase-keys npm package is a utility that converts the keys of an object to snake_case. This can be particularly useful when working with APIs or databases that require snake_case formatting.
What are snakecase-keys's main functionalities?
Convert Object Keys to Snake Case
This feature allows you to convert the keys of an object from camelCase to snake_case. It works recursively, so nested objects are also converted.
const snakecaseKeys = require('snakecase-keys');
const camelCaseObject = { fooBar: 'baz', nestedObject: { helloWorld: 'value' } };
const snakeCaseObject = snakecaseKeys(camelCaseObject);
console.log(snakeCaseObject); // { foo_bar: 'baz', nested_object: { hello_world: 'value' } }
Exclude Specific Keys
This feature allows you to exclude specific keys from being converted to snake_case. You can pass an array of keys to the `exclude` option.
const snakecaseKeys = require('snakecase-keys');
const camelCaseObject = { fooBar: 'baz', nestedObject: { helloWorld: 'value' }, excludeMe: 'no_change' };
const snakeCaseObject = snakecaseKeys(camelCaseObject, { exclude: ['excludeMe'] });
console.log(snakeCaseObject); // { foo_bar: 'baz', nested_object: { hello_world: 'value' }, excludeMe: 'no_change' }
Deep Conversion
This feature ensures that all nested objects are also converted to snake_case. By default, the conversion is deep, but you can control this behavior with the `deep` option.
const snakecaseKeys = require('snakecase-keys');
const camelCaseObject = { fooBar: 'baz', nestedObject: { helloWorld: 'value' } };
const snakeCaseObject = snakecaseKeys(camelCaseObject, { deep: true });
console.log(snakeCaseObject); // { foo_bar: 'baz', nested_object: { hello_world: 'value' } }
Other packages similar to snakecase-keys
camelcase-keys
The camelcase-keys package converts object keys to camelCase. It is the inverse of snakecase-keys and can be useful when you need to convert keys from snake_case to camelCase.
lodash
Lodash is a utility library that provides a wide range of functions for manipulating objects, arrays, and other data types. It includes a `_.snakeCase` function that can be used to convert strings to snake_case, but it does not provide a direct way to convert object keys like snakecase-keys.
change-case
The change-case package provides a set of functions for converting strings between different cases, including snake_case. While it does not directly convert object keys, it can be used in combination with other utilities to achieve similar functionality.
snakecase-keys
Convert an object's keys to snake case
Install
$ npm install --save snakecase-keys
Usage
var snakecaseKeys = require('snakecase-keys')
snakecaseKeys({fooBar: 'baz'})
snakecaseKeys({'foo-bar': true, nested: {fooBaz: 'bar'}});
API
snakecaseKeys(obj, options)
-> object
obj
Required
Type: object
An object to transform into snake case (keys only).
options
Optional
Type: object
deep
Type: boolean
Default: true
Enables snake-casing of keys in nested objects.
exclude
Type: array[string || regexp]
Default: []
An array of strings or regular expressions matching keys that will be excluded from snake-casing.
Related
License
MIT © Ben Drucker