-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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