Skip to content

Instantly share code, notes, and snippets.

@TheJarX
Last active March 23, 2021 21:40
Show Gist options
  • Save TheJarX/99d35bdd513c73229c270d7216b2407d to your computer and use it in GitHub Desktop.
Save TheJarX/99d35bdd513c73229c270d7216b2407d to your computer and use it in GitHub Desktop.
Function to convert camel case hash to snake case
const keyToSnake = word => word.replace(
/[A-Z]+/,
match => `_${match.toLowerCase().split('').join('_')}`,
);
const toSnakeCase = (obj) => {
const entries = Object.entries(obj).map(
entry => [keyToSnake(entry[0]), entry[1]],
);
return Object.fromEntries(entries);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment