Skip to content

Instantly share code, notes, and snippets.

@ashish-r
Created July 30, 2020 05:39
Show Gist options
  • Save ashish-r/7cea3e7a96581dddc8b6bfd496f791d6 to your computer and use it in GitHub Desktop.
Save ashish-r/7cea3e7a96581dddc8b6bfd496f791d6 to your computer and use it in GitHub Desktop.
Function to convert object from snakeCase keys to camelCase keys
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