0.1.3 • Published 6 years ago

format-object-keys v0.1.3

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

format-object-keys CircleCI

Format keys in an object to be camelCase, PascalCase, or a custom format.

Install

yarn add format-object-keys

Example

// import function
const formatObjectKeys = require('format-object-keys')

// provide a data structure whose
// keys you want formatted
const dataStructure = {
  FIRST_NAME: 'Kunal',
  LAST_NAME: 'Mandalia',
  LOCATION: 'London',
  JOB_HISTORY: [
    {
      DATE_FROM: 'April 2016',
      DATE_TO: 'Present',
      TITLE: 'Js developer'
    }
  ]
}

// define a function to format keys
// here we'll camelCase key given
// key has the structure <FIRSTWORD>_<SECONDWORD>...
const formatter = key => {
  if (typeof key === 'string') {
    return key.toLowerCase()
      .split('_')
      .map((word, i) => i > 0
        ? word[0].toUpperCase() + word.substr(1)
        : word
      )
      .join('')
  }
  return key
}

const result = formatObjectKeys(dataStructure, formatter)
console.log(result)
// {
//   firstName: 'Kunal',
//   lastName: 'Mandalia',
//   location: 'London',
//   jobHistory: [
//     {
//       dateFrom: 'April 2016',
//       dateTo: 'Present',
//       title: 'Js developer'
//     }
//   ]
// }

Limitations

No support for circular references yet. PRs welcome to support this feature.

License

MIT

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago