Skip to content

Instantly share code, notes, and snippets.

@HugoDF
Created June 8, 2018 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoDF/bdd2a2475cd456c35bab8565e4e1b13e to your computer and use it in GitHub Desktop.
Save HugoDF/bdd2a2475cd456c35bab8565e4e1b13e to your computer and use it in GitHub Desktop.
Map snake_case to camelCase recursively
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