Skip to content

Instantly share code, notes, and snippets.

@azinasili
Created April 12, 2019 17:56
Show Gist options
  • Save azinasili/c031fd777ce001e0401de029dada2ee8 to your computer and use it in GitHub Desktop.
Save azinasili/c031fd777ce001e0401de029dada2ee8 to your computer and use it in GitHub Desktop.
Takes a string and converts to pascalCase
/**
* 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