Created
July 30, 2020 05:39
-
-
Save ashish-r/7cea3e7a96581dddc8b6bfd496f791d6 to your computer and use it in GitHub Desktop.
Function to convert object from snakeCase keys to camelCase keys
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
const convertKeysToCamelCase = (obj, onlyAtRootLevel) => | |
Object.fromEntries( | |
Object.entries(obj).map(([key, val]) => [ | |
key.replace(/([_][a-z])/g, (group) => group.toUpperCase().replace('_', '')), | |
onlyAtRootLevel || !(val instanceof Object) ? val : convertKeysToCamelCase(val) | |
]) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment