Skip to content

Instantly share code, notes, and snippets.

@gregogalante
Forked from emcmanus/snakeToCamelCase.js
Last active March 5, 2018 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregogalante/46999ff233c92c8ed243050f85a4cd0a to your computer and use it in GitHub Desktop.
Save gregogalante/46999ff233c92c8ed243050f85a4cd0a to your computer and use it in GitHub Desktop.
ES6 module to recursively convert snake case keys in an object to camel case using lodash.
export function camelCaseKeys(object) {
let camelCaseObject = _.cloneDeep(object)
if (_.isArray(camelCaseObject)) {
return _.map(camelCaseObject, camelCaseKeys)
}
if (_.isString(camelCaseObject)) {
return camelCaseObject
}
camelCaseObject = _.mapKeys(camelCaseObject, (value, key) => _.camelCase(key))
// Recursively apply throughout object
return _.mapValues(camelCaseObject, (value) => {
if (_.isPlainObject(value)) {
return camelCaseKeys(value)
} else if (_.isArray(value)) {
return _.map(value, camelCaseKeys)
}
return value
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment