Created
October 21, 2018 08:22
-
-
Save vilindberg/22e68418250a599777dac80b9c1c4f82 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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