Created
July 30, 2018 23:24
-
-
Save gladchinda/800726faa745ceabe49a82cb835c4ce0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 snakeCase = value => { | |
| const regex = /[A-Z][^A-Z]+/g; | |
| const withoutSpaces = value.trim().replace(/\s+/g, '_'); | |
| const caps = withoutSpaces.match(regex); | |
| const splits = withoutSpaces.split(regex); | |
| let finalString = splits.shift(); | |
| for (let i = 0; i < splits.length; i++) { | |
| finalString += `${caps[i]}_${splits[i]}_`; | |
| } | |
| return finalString | |
| .toLowerCase() | |
| .replace(/_+/g, '_') | |
| .replace(/^_?(.+?)_?$/, '$1'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment