Created
June 8, 2018 10:56
-
-
Save HugoDF/bdd2a2475cd456c35bab8565e4e1b13e to your computer and use it in GitHub Desktop.
Map snake_case to camelCase recursively
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 delimiterRegEx = /(_\w)/g; | |
export function convertSnakeToCamelCase (str) { | |
return str.replace(delimiterRegEx, ([delimiter, firstChar]) => | |
firstChar.toUpperCase() | |
); | |
} | |
export const convertKeysToCamelCase = obj => | |
Object.entries(obj).reduce((prev, [key, value]) => { | |
if (value && typeof value === 'object') { | |
prev[convertSnakeToCamelCase(key)] = convertKeysToCamelCase(value); | |
} else { | |
prev[convertSnakeToCamelCase(key)] = value; | |
} | |
return prev; | |
}, {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment