CamelCase to snake_case Javascript
Convert all your objects with camelCase properties to snake_case 👀🎉
Install
npm install camelcase-to-snakecase
yarn add camelcase-to-snakecase
unpkg
https://unpkg.com/camelcase-to-snakecase@latest/src/index.js
jsdelivr
https://cdn.jsdelivr.net/npm/camelcase-to-snakecase@latest/src/index.min.js
Import
const { camelCaseToSnakeCase, replaceCamelCaseObj } = require('camelcase-to-snakecase');
Usage
// Converting a camelCase object to snake_case
const objInCamelCase = {
userName: 'josue_ayala',
favoriteColors: ['red', 'green', 'blue'],
other: { firstName: 'Josue', lastName: 'Ayala' }
};
console.log(replaceCamelCaseObj(objInCamelCase));
/* returns
{
user_name: 'josue_ayala',
favorite_colors: [ 'red', 'green', 'blue' ],
other: { first_name: 'Josue', last_name: 'Ayala' }
}
*/
// Converting a string camelCase to snake_case
const camelCase = 'thisIsMyCamelCaseConst';
console.log(camelCaseToSnakeCase(camelCase));
/* returns
this_is_my_camel_case_const
*/
License
MIT