Skip to content

Instantly share code, notes, and snippets.

@capJavert
Last active April 28, 2020 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capJavert/20bb8e7358dad657cb6692f8ce16f977 to your computer and use it in GitHub Desktop.
Save capJavert/20bb8e7358dad657cb6692f8ce16f977 to your computer and use it in GitHub Desktop.
Convert delimiter based string like hyphen-case to pascalCase string
const capitalize = text => text.replace(/^\w/, c => c.toUpperCase())
/**
* Convert delimiter based string like hyphen-case to
* pascalCase string
*
* @param {*} text
* @param {string} [delimiter='-'] delimiter regex, for / you need to pass \/
* @returns {string} pascal cased string
*/
const pascalCase = (text, delimiter = '-') => {
if (!text) {
return ''
}
const regex = new RegExp(`/${delimiter}([a-z])/g`)
return capitalize(
text.replace(regex, (_m, s) => {
return s.toUpperCase()
})
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment