Last active
June 25, 2019 15:46
-
-
Save paulhhowells/86d8e05ab167f34c7ee34280d2adfcfe to your computer and use it in GitHub Desktop.
snake_case camelCase conversion
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 notAlphaNumericUnderscoreNorWhitespace = /[^\w\s]/g; | |
const whitespace = /\W+/g; | |
function camelCase (string) { | |
return string | |
.trim() | |
.replace(notAlphaNumericUnderscoreNorWhitespace, '') | |
.split(whitespace) | |
.map((string, index) => { | |
return (index) | |
? string.charAt(0).toUpperCase() + string.slice(1) | |
: string.toLowerCase(); | |
}) | |
.join(''); | |
} | |
function snakeCase (string) { | |
return string | |
.trim() | |
.replace(notAlphaNumericUnderscoreNorWhitespace, '') | |
.replace(whitespace, '_') | |
.toLowerCase(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment