Skip to content

Instantly share code, notes, and snippets.

@intruxxer
Forked from johnsmith17th/camelCase.js
Last active June 19, 2017 15:28
Show Gist options
  • Save intruxxer/3abf890efb0419384cde1441bf8a457c to your computer and use it in GitHub Desktop.
Save intruxxer/3abf890efb0419384cde1441bf8a457c to your computer and use it in GitHub Desktop.
To convert string to camel case in javascript.
//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