Last active
March 23, 2021 21:40
-
-
Save TheJarX/99d35bdd513c73229c270d7216b2407d to your computer and use it in GitHub Desktop.
Function to convert camel case hash to snake case
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 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