Skip to content

Instantly share code, notes, and snippets.

@aslanon
Last active March 22, 2020 14:37
Show Gist options
  • Save aslanon/9ae2adc4584a9e72a935e776484367f3 to your computer and use it in GitHub Desktop.
Save aslanon/9ae2adc4584a9e72a935e776484367f3 to your computer and use it in GitHub Desktop.
String transform
/**
* To capitalize
* @param {String} string
* return example text -> Example text
*/
const capitalize = string =>
string && string.charAt(0).toUpperCase() + string.slice(1);
/**
* To camel case
* @param {String} string
* return example-text -> ExampleText
*/
const camelCase = string =>
string &&
string
.toLowerCase()
.trim()
.split(/[.\-_\s]/g)
.reduce((string, word) => string + word[0].toUpperCase() + word.slice(1));
/**
* To kebab case
* @param {String} string
* return example Text -> example-text
*/
const kebabCase = string =>
string &&
string
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(x => x.toLowerCase())
.join("-");
export { capitalize, camelCase, kebabCase };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment