snake-to-camel-converter
v1.0.3
Published
A simple npm package for converting object keys from snake_case to camelCase.
Downloads
3
Readme
snake-to-camel-converter
A simple npm package for converting object keys from snake_case to camelCase.
Installation
npm install snake-to-camel-converter
Usage
const snakeToCamelConverter = require('snake-to-camel-converter');
// String Case Conversions
const inputString = "this_is_a_test_string";
const camelCaseString = snakeToCamelConverter.convertToCamelCase(inputString);
console.log(camelCaseString);
const snakeCaseString = snakeToCamelConverter.convertToSnakeCase(camelCaseString);
console.log(snakeCaseString);
const pascalCaseString = snakeToCamelConverter.convertToPascalCase(snakeCaseString);
console.log(pascalCaseString);
const kebabCaseString = snakeToCamelConverter.convertToKebabCase(pascalCaseString);
console.log(kebabCaseString);
// Object Key Conversions
const inputObject = {
first_name: 'John',
last_name: 'Doe',
contact_info: {
email_address: '[email protected]',
phone_number: '123-456-7890'
}
};
const camelCaseObject = snakeToCamelConverter.convertToCamelCase(inputObject);
console.log(camelCaseObject);
const snakeCaseObject = snakeToCamelConverter.convertToSnakeCase(camelCaseObject);
console.log(snakeCaseObject);
const pascalCaseObject = snakeToCamelConverter.convertToPascalCase(snakeCaseObject);
console.log(pascalCaseObject);
const kebabCaseObject = snakeToCamelConverter.convertToKebabCase(pascalCaseObject);
console.log(kebabCaseObject);
Example
Input
const inputObject = {
my_key: 'value',
nested_object: {
another_key: 'another_value'
}
};
Output
{
myKey: 'value',
nestedObject: {
anotherKey: 'another_value'
}
}
Input with array
const inputObject = {
my_key: 'value',
nested_object: {
another_key: 'another_value'
},
my_array: [
{
my_key: 'value',
nested_object: {
another_key: 'another_value'
}
},
{
my_key: 'value',
nested_object: {
another_key: 'another_value'
}
}
]
};
Output with array
{
myKey: 'value',
nestedObject: {
anotherKey: 'another_value'
},
myArray: [
{
myKey: 'value',
nestedObject: {
anotherKey: 'another_value'
}
},
{
myKey: 'value',
nestedObject: {
anotherKey: 'another_value'
}
}
]
};
Example with JSON
{
"first_name": "John",
"last_name": "Doe",
"contact_info": {
"email_address": "[email protected]",
"phone_number": "123-456-7890"
}
}
Output with JSON
{
"firstName": "John",
"lastName": "Doe",
"contactInfo": {
"emailAddress": "[email protected]",
"phoneNumber": "123-456-7890"
}
}
License
This project is licensed under the MIT License