Last active
March 22, 2020 14:37
-
-
Save aslanon/9ae2adc4584a9e72a935e776484367f3 to your computer and use it in GitHub Desktop.
String transform
This file contains hidden or 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
/** | |
* 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