Last active
April 28, 2020 14:02
-
-
Save capJavert/20bb8e7358dad657cb6692f8ce16f977 to your computer and use it in GitHub Desktop.
Convert delimiter based string like hyphen-case to pascalCase string
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 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