Last active
August 21, 2016 15:43
-
-
Save thysultan/28d59f72b380968a050395bbc59580b3 to your computer and use it in GitHub Desktop.
camelCase and undoCamelCase
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
function camelCase (string, separator) { | |
return string.split(separator).map((word, index) => word.substr(0,1)[index ? 'toUpperCase' : 'toLowerCase']() + word.substr(1) ).join(''); | |
} | |
console.log(camelCase('metal gear solid', ' ')); | |
// metalGearSolid | |
function undoCamelCase (string, separator) { | |
return string.split('').map((letter) => letter.toLowerCase() !== letter ? separator + letter.toLowerCase() : letter).join(''); | |
} | |
console.log(undoCamelCase('helloWorld', '_')); | |
// => hello_world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment