Last active
June 15, 2018 15:16
-
-
Save usrbowe/69bf2dce2c00390c7e056f9595c159bc to your computer and use it in GitHub Desktop.
Camelize string in Javascript / ES6 💪
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
/* Transform dasherized, underscored string to camelCase */ | |
const cameliseMe = (str, separator = "-") => { | |
const splitted = str.split(separator); | |
if (splitted.length === 1) return str; | |
return splitted.reduce( | |
(acc, [up, ...rest]) => `${acc}${up.toUpperCase() + rest.join("")}` | |
); | |
}; | |
/* Basic example | |
cameliseMe('this-is-easy-dasherized-string'); | |
// OUTPUT: | |
'thisIsEasyDasherizedString' | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also possible to add
sanitize
option which would clean any white-spaces, and mixed lower/upper case of characters.Like this:
ThIs- is- CompleteLy-WroNG