Skip to content

replygirl/change-case-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

change-case-object

Recursively change the case of an object's keys

Installation

yarn add @replygirl/change-case-object

Usage

Functions

import { /* function */ } from 'change-case-object'

camelCase

camelCase({ some_key: { some_other_key: true })
// { someKey: { someOtherKey: true }}

capitalCase

capitalCase({ some_key: { some_other_key: true })
// { 'Some Key': { 'Some Other Key': true }}

constantCase

constantCase({ some_key: { some_other_key: true })
// { SOME_KEY: { SOME_OTHER_KEY: true }}

dotCase

Why would you do this??

dotCase({ some_key: { some_other_key: true })
// { 'some.key': { 'some.other.key': true }}

headerCase

headerCase({ some_key: { some_other_key: true })
// { 'Some-Key': { 'Some-Other-Key': true }}

noCase

noCase({ some_key: { some_other_key: true })
// { 'some key': { 'some other key': true }}

paramCase

paramCase({ some_key: { some_other_key: true })
// { 'some-key': { 'some-other-key': true }}

pascalCase

pascalCase({ some_key: { some_other_key: true })
// { SomeKey: { SomeOtherKey: true }}

pathCase

pathCase({ some_key: { some_other_key: true })
// { 'some/key': { 'some/other/key': true }}

sentenceCase

sentenceCase({ some_key: { some_other_key: true })
// { 'Some key': { 'Some other key': true }}

snakeCase

snakeCase({ 'some key': { 'some other key': true })
// { some_key: { some_other_key: true }}

CasedObject class

import { CasedObject /*, Case */ } from 'change-case-object'

Static functions

All of the individual functions are available as static members.

CasedObject.camelCase({ some_key: { some_other_key: true }})
// { someKey: { someOtherKey: true }}

Instances

const someObj = { some_key: { 'some other key': true }}

// initialize without changing case
const foo = new CasedObject(someObj)
console.info(foo.value) // { some_key: { 'some other key': true }}

// initialize and change case
const bar = new CasedObject(someObj, { case: 'camel' })
console.info(bar.value) // { someKey: { someOtherKey: true }}

// initialize and change case using enum
const baz = new CasedObject(someObj, { case: Case.camel })
console.info(baz.value) // { someKey: { someOtherKey: true }}

// output the value with keys in a different case
console.info(baz.snakeCase) // { some_key: { some_other_key: true }}
console.info(baz.value) // { someKey: { someOtherKey: true }}

// change the case of the value's keys
baz.value = baz.snakeCase
console.info(baz.value) // { some_key: { some_other_key: true }}

License

ISC (c) 2020 replygirl