Skip to content

Instantly share code, notes, and snippets.

@Kirens
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save Kirens/358a24fa7fea685c9338 to your computer and use it in GitHub Desktop.

Select an option

Save Kirens/358a24fa7fea685c9338 to your computer and use it in GitHub Desktop.
Turns a string into CamelCase, eg. "A string into CamelCase" -> "aStringIntoCamelCase"
var stringToCamelCase = function(string){
if(typeof string !== 'string')throw "Must be string";
//first character to lower case
string = string.charAt(0).toLowerCase() + string.slice(1);
//capitalize first letter after whitespace
return string.replace(/\s(\w)/g,function(){
return arguments[1].toUpperCase();
},'g');
}
var camelCaseToUpper = function(string){
if(typeof string !== 'string')throw "Must be string";
for(var i=0;i<string.length;i++)
if(string.charAt(i) == string.charAt(i).toUpperCase())
string = string.substr(0,i)+'_'+string.substr(i++);
return string.toUpperCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment