Created
April 12, 2019 17:56
-
-
Save azinasili/c031fd777ce001e0401de029dada2ee8 to your computer and use it in GitHub Desktop.
Takes a string and converts to pascalCase
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
/** | |
* Convert string to pascalCase. | |
* Function will remove `,`, `_`, `-`, and `' '` from string. | |
* | |
* Note: Function does not strip numeric characters. | |
* A string like `hello 1world` would return `Hello1world` | |
* | |
* @example | |
* pascalCase('hello world') // returns 'HelloWorld' | |
* | |
* @example | |
* pascalCase('hello_world') // returns 'HelloWorld' | |
* | |
* @example | |
* pascalCase('hello-world') // returns 'HelloWorld' | |
* | |
* @example | |
* pascalCase('hello,world') // returns 'HelloWorld' | |
* | |
* @example | |
* pascalCase('__--HeLlO wOrLd--__') // returns 'HelloWorld' | |
* | |
* @param {string} string - String to convert | |
* @returns {string} | |
*/ | |
function pascalCase(string) { | |
const strArry = string.split(/[ ,_-]+/).filter(Boolean); | |
const capitalize = strArry.map(str => str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()); | |
return capitalize.join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment