-
-
Save intruxxer/3abf890efb0419384cde1441bf8a457c to your computer and use it in GitHub Desktop.
To convert string to camel case in javascript.
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
//Original | |
//e.g. my life is good => MyLifeIsGood | |
function toCamelCase(str) { | |
return str.toLowerCase().replace(/(?:(^.)|(\s+.))/g, function(match) { | |
return match.charAt(match.length-1).toUpperCase(); | |
}); | |
} | |
//Forked: Capable to Handle > 1 word/Phrase | |
//e.g. my life is good => My Life Is Good | |
function toCamelCase(str) { | |
var camelized = ""; | |
var splitted = str.split(" "); | |
var splitted_camelized; | |
for (i = 0; i < splitted.length; i++) { | |
splitted_camelized = splitted[i].toLowerCase().replace(/(?:(^.)|(\s+.))/g, function(match) { | |
return match.charAt(match.length-1).toUpperCase(); | |
}); | |
if(i == splitted.length - 1) | |
camelized = camelized + splitted_camelized + " "; | |
else | |
camelized = camelized + splitted_camelized + " "; | |
} | |
return camelized; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment