Last active
August 29, 2015 14:16
-
-
Save Kirens/358a24fa7fea685c9338 to your computer and use it in GitHub Desktop.
Turns a string into CamelCase, eg. "A string into CamelCase" -> "aStringIntoCamelCase"
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
| 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