Skip to content

Instantly share code, notes, and snippets.

@paulhhowells
Last active June 25, 2019 15:46
Show Gist options
  • Save paulhhowells/86d8e05ab167f34c7ee34280d2adfcfe to your computer and use it in GitHub Desktop.
Save paulhhowells/86d8e05ab167f34c7ee34280d2adfcfe to your computer and use it in GitHub Desktop.
snake_case camelCase conversion
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