changecase-objects

Convert an object's keys to any case

Usage no npm install needed!

<script type="module">
  import changecaseObjects from 'https://cdn.skypack.dev/changecase-objects';
</script>

README

changecase-objects

Convert an object's keys to different cases

Installation

$ npm install --save changecase-objects

Usage

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' }}

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
camelCase({ foo_bar: 'baz', 'filters[foo_bar]': 'val' }, { exclude: ['filters[foo_bar]'] })
// 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.

camelCase({ 'key name': 'val' })
// 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.

constantCase({ 'key name': 'val' })
// 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.

dotCase({ 'key name': 'val' })
// 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.

headerCase({ 'key name': 'val' })
// 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.

kebabCase({ 'key name': 'val' })
// 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.

lowerCase({ 'Key Name': 'val' })
// 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.

pascalCase({ 'key name': 'val' })
// 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.

pathCase({ 'key name': 'val' })
// 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.

sentenceCase({ 'key name': 'val' })
// 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.

snakeCase({ 'key name': 'val' })
// 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.

titleCase({ 'key name': 'val' })
// 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.

upperCase({ 'key name': 'val' })
// 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.

customCase({ 'key name': 'val' }, key => transform(key))
// 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