changecase-objects
Convert an object's keys to different cases
Installation
$ npm install --save changecase-objects
Usage
var changeKeys =changeKeys// returns { foo_bar: 'baz' }changeKeys// returns { foo_bar: true, nested: { foo_baz: 'bar' }}
or with imports
// returns { foo_bar: 'baz' }// returns { foo_bar: true, nested: { foo_baz: 'bar' }}
API
Extra Options
Every function accepts an optional options object. The following options are currently supported:
Name | Type | Description |
---|---|---|
exclude | array<string> |
Array of key values to exclude from case change. |
exclude
// returns { fooBar: 'baz', 'filters[foo_bar]': 'val' }
camelCase
camelCase(obj, [options]) -> Object
Converts keys to a string with the separators denoted by having the next letter capitalized.
// returns { keyName: 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into camel case. |
options | Object |
No | see options |
CONSTANT_CASE
constantCase(obj, [options]) -> Object
Converts keys to upper case, with an underscore separator.
// returns { KEY_NAME: 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into constant case. |
options | Object |
No | see options |
dot.case
dotCase(obj, [options]) -> Object
Converts keys to lower case, with a period separator.
// returns { 'key.name': 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into dot case. |
options | Object |
No | see options |
Header-Case
headerCase(obj, [options]) -> Object
Converts keys to title case, with a dash separator.
// returns { 'Key-Name': 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into header case. |
options | Object |
No | see options |
kebab-case
kebabCase(obj, [options]) -> Object
Converts keys to lower case, with a dash separator.
// returns { 'key-name': 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into kebab case. |
options | Object |
No | see options |
lower case
lowerCase(obj, [options]) -> Object
Converts keys to lower case, with a space separator.
// returns { 'key name': 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into lower case. |
options | Object |
No | see options |
PascalCase
pascalCase(obj, [options]) -> Object
Converts keys to camel case, with the first character also capitalized.
// returns { KeyName: 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into pascal case. |
options | Object |
No | see options |
path/case
pathCase(obj, [options]) -> Object
Converts keys to lower case, with a slash separator.
// returns { 'key/name': 'val' })
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into path case. |
options | Object |
No | see options |
Sentence case
sentenceCase(obj, [options]) -> Object
Converts keys to lower case, with a space separator, with the first letter capitalized.
// returns { 'Key name': 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into sentence case. |
options | Object |
No | see options |
snake_case
snakeCase(obj, [options]) -> Object
Converts keys to lower case, with an underscore separator.
// returns { key_name: 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into snake case. |
options | Object |
No | see options |
Title Case
titleCase(obj, [options]) -> Object
Converts keys to lower case with the first letter of each word capitalized, with a space separator.
// returns { 'Key Name': 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into title case. |
options | Object |
No | see options |
UPPER CASE
upperCase(obj, [options]) -> Object
Converts keys to upper case, with a space separator.
// returns { 'KEY NAME: 'val' }
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into upper case. |
options | Object |
No | see options |
-cUsToM-cAsE-
customCase(obj, caseFn, [options]) -> Object
Allows for custom casing rules by converting keys according to given function.
// returns keys formatted by `caseFn`
Parameters
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into upper case. |
caseFn | function(string) -> string |
Yes | A function that returns a modified key. |
options | Object |
No | see options |