Skip to content

Instantly share code, notes, and snippets.

@vilindberg
Created October 21, 2018 08:22
Show Gist options
  • Save vilindberg/22e68418250a599777dac80b9c1c4f82 to your computer and use it in GitHub Desktop.
Save vilindberg/22e68418250a599777dac80b9c1c4f82 to your computer and use it in GitHub Desktop.
const stringToCamel = (str: string) => {
str = str.replace(/[-_\s]+(.)?/g, (_, ch) => (ch ? ch.toUpperCase() : ''))
return str.substr(0, 1).toLowerCase() + str.substr(1)
}
const objectToCamel = (obj: Object) =>
Object.keys(obj).reduce((tutti, prop) => {
tutti[stringToCamel(prop)] = obj[prop]
return tutti
}, {})
export const toCamelCase = (input: Object | Object[]) => {
if (Array.isArray(input)) {
return input.map(obj => objectToCamel(obj))
}
return objectToCamel(input)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment