$ npm install --save changecase-objects
var changeKeys = require('changecase-objects')
changeKeys.snakeCase({ fooBar: 'baz' })
// returns { foo_bar: 'baz' }
changeKeys.snakeCase({ 'foo-bar': true, nested: { fooBaz: 'bar' }})
// returns { foo_bar: true, nested: { foo_baz: 'bar' }}
or with imports
import { snakeCase } from 'changecase-objects'
snakeCase({ fooBar: 'baz' })
// returns { foo_bar: 'baz' }
snakeCase({ 'foo-bar': true, nested: { fooBaz: 'bar' }})
// returns { foo_bar: true, nested: { foo_baz: 'bar' }}
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. |
camelCase({ foo_bar: 'baz', 'filters[foo_bar]': 'val' }, { exclude: ['filters[foo_bar]'] })
// returns { fooBar: 'baz', 'filters[foo_bar]': 'val' }
camelCase(obj, [options]) -> Object
Converts keys to a string with the separators denoted by having the next letter capitalized.
camelCase({ 'key name': 'val' })
// returns { keyName: 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into camel case. |
options | Object |
No | see options |
constantCase(obj, [options]) -> Object
Converts keys to upper case, with an underscore separator.
constantCase({ 'key name': 'val' })
// returns { KEY_NAME: 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into constant case. |
options | Object |
No | see options |
dotCase(obj, [options]) -> Object
Converts keys to lower case, with a period separator.
dotCase({ 'key name': 'val' })
// returns { 'key.name': 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into dot case. |
options | Object |
No | see options |
headerCase(obj, [options]) -> Object
Converts keys to title case, with a dash separator.
headerCase({ 'key name': 'val' })
// returns { 'Key-Name': 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into header case. |
options | Object |
No | see options |
kebabCase(obj, [options]) -> Object
Converts keys to lower case, with a dash separator.
kebabCase({ 'key name': 'val' })
// returns { 'key-name': 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into kebab case. |
options | Object |
No | see options |
lowerCase(obj, [options]) -> Object
Converts keys to lower case, with a space separator.
lowerCase({ 'Key Name': 'val' })
// returns { 'key name': 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into lower case. |
options | Object |
No | see options |
pascalCase(obj, [options]) -> Object
Converts keys to camel case, with the first character also capitalized.
pascalCase({ 'key name': 'val' })
// returns { KeyName: 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into pascal case. |
options | Object |
No | see options |
pathCase(obj, [options]) -> Object
Converts keys to lower case, with a slash separator.
pathCase({ 'key name': 'val' })
// returns { 'key/name': 'val' })
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into path case. |
options | Object |
No | see options |
sentenceCase(obj, [options]) -> Object
Converts keys to lower case, with a space separator, with the first letter capitalized.
sentenceCase({ 'key name': 'val' })
// returns { 'Key name': 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into sentence case. |
options | Object |
No | see options |
snakeCase(obj, [options]) -> Object
Converts keys to lower case, with an underscore separator.
snakeCase({ 'key name': 'val' })
// returns { key_name: 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into snake case. |
options | Object |
No | see options |
titleCase(obj, [options]) -> Object
Converts keys to lower case with the first letter of each word capitalized, with a space separator.
titleCase({ 'key name': 'val' })
// returns { 'Key Name': 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into title case. |
options | Object |
No | see options |
upperCase(obj, [options]) -> Object
Converts keys to upper case, with a space separator.
upperCase({ 'key name': 'val' })
// returns { 'KEY NAME: 'val' }
Name | Type | Required | Description |
---|---|---|---|
obj | Object or array<Object> |
Yes | An object to transform keys into upper case. |
options | Object |
No | see options |
customCase(obj, caseFn, [options]) -> Object
Allows for custom casing rules by converting keys according to given function.
customCase({ 'key name': 'val' }, key => transform(key))
// returns keys formatted by `caseFn`
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 |