A set of utility functions converting the case of strings.
npm install @mmckelvy/case
npm install @mmckelvy/case
`Usage
Convert the case of a word:`javascript
const { camelCase, snakeCase } = require('@mmckelvy/case');camelCase('first_name');
// -> 'firstName'
snakeCase('firstName');
// -> 'first_name'
properCase('first_name');
// -> 'First name'
titleCase('first_name');
// -> 'First Name'
`Handles kabob-case, extra characters, and spaces as well:
`javascript
camelCase('first-name');
// -> 'firstName'snakeCase('First--Name');
// -> 'first_name'
`A frequent use case for case conversion is converting the case of object keys.
case comes with camelCaseKeys and snakeCaseKeys helper methods to handle this use case:`javascript
const { camelCaseKeys, snakeCaseKeys } = require('@mmckelvy/case');const snakeToCamel = {
first_name: 'John',
last_name: 'Smith'
};
camelCaseKeys(snakeToCamel);
/* ->
{
firstName: 'John',
lastName: 'Smith'
};
*/
const camelToSnake = {
firstName: 'John',
lastName: 'Smith'
};
snakeCaseKeys(camelToSnake);
/* ->
{
first_name: 'John',
last_name: 'Smith'
};
*/
`
Works with arrays:`javascript
const input = [
{
first_name: 'John',
last_name: 'Smith'
},
{
first_name: 'Jane',
last_name: 'Jenkins'
},
];camelCaseKeys(input);
/* ->
[
{
firstName: 'John',
lastName: 'Smith'
},
{
firstName: 'Jane',
lastName: 'Jenkins'
},
];
*/
`To handle nested objects, pass
{recursive: true} as the second argument:`javascript
const input = {
first_name: 'John',
last_name: 'Smith',
address: {
country_of_residence: 'USA',
}
};camelCaseKeys(input, {recursive: true});
/* ->
{
firstName: 'John',
lastName: 'Smith',
address: {
countryOfResidence: 'USA'
}
};
*/
`API
$3
Convert a string to camelCase.#### str
stringThe string to convert.
#### return
stringA new string in camelCase.
$3
Convert a string to snake_case.#### str
stringThe string to convert.
#### return
stringA new string in snake_case.
$3
Convert a string to Proper case.#### str
stringThe string to convert.
#### return
stringA new string in Proper case.
$3
Convert a string to Title Case.#### str
stringThe string to convert.
#### return
stringA new string in Title Case.
$3
Convert object keys to camelCase.#### input
object or object[]An object or array of objects with keys you'd like to convert.
#### options (optional)
object##### options.recursive
booleanRecursively convert keys for nested objects and arrays.
#### return
A new
object or object[] (does not mutate the original object or object[]).
$3
Convert object keys to snake_case.#### input
object or object[]An object or array of objects with keys you'd like to convert.
#### options (optional)
object##### options.recursive
booleanRecursively convert keys for nested objects and arrays.
#### return
A new
object or object[] (does not mutate the original object or object[]).
$3
Standardize two strings and then compare them.#### a
stringThe first string to compare.
#### b
stringThe second string to compare.
#### fn (optional)
functionA function applied to each string to standardize it. Defaults to
snakeCase.#### return
booleantrue if the strings are === after applying the standardization function, else false`.