format-object-keys

0.1.3 • Public • Published

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

Package Sidebar

Install

npm i format-object-keys

Weekly Downloads

0

Version

0.1.3

License

MIT

Unpacked Size

118 kB

Total Files

7

Last publish

Collaborators

  • kunal-mandalia