Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
snakecase-keys
Advanced tools
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.
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' } }
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 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.
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.
Convert an object's keys to snake case
$ npm install --save snakecase-keys
var snakecaseKeys = require('snakecase-keys')
snakecaseKeys({fooBar: 'baz'})
//=> {foo_bar: 'baz'}
snakecaseKeys({'foo-bar': true, nested: {fooBaz: 'bar'}});
//=> {foo_bar: true, nested: {foo_baz: 'bar'}}
snakecaseKeys(obj, options)
-> object
Required
Type: object | array[object]
A plain object or array of plain objects to transform into snake case (keys only).
Optional
Type: object
Type: boolean
Default: true
Enables snake-casing of keys in nested objects.
Type: array[string || regexp]
Default: []
An array of strings or regular expressions matching keys that will be excluded from snake-casing.
shouldRecurse(key, val)
-> boolean
Optional
Type: function
A function that determines if val
should be recursed.
Requires deep: true
.
MIT © Ben Drucker
FAQs
Convert an object's keys to snake case
The npm package snakecase-keys receives a total of 487,481 weekly downloads. As such, snakecase-keys popularity was classified as popular.
We found that snakecase-keys demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.