New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

format-object-keys

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

format-object-keys

format javascript object keys e.g. to camelCase

latest
Source
npmnpm
Version
0.1.3
Version published
Maintainers
1
Created
Source

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

Keywords

format

FAQs

Package last updated on 15 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts