-
-
Save hernan/e46fecafa6b108fd19bb245772af5090 to your computer and use it in GitHub Desktop.
Camelize a string in Javascript. Example: camelize("hello_world") --> "HelloWorld"
This file contains 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
/** | |
* Camelize a string, cutting the string by separator character. | |
* @param string Text to camelize | |
* @param string Word separator (underscore by default) | |
* @return string Camelized text | |
*/ | |
function camelize(text, separator) { | |
// Assume separator is _ if no one has been provided. | |
if(typeof(separator) == "undefined") { | |
separator = "_"; | |
} | |
// Cut the string into words | |
var words = text.split(separator); | |
// Concatenate all capitalized words to get camelized string | |
var result = ""; | |
for (var i = 0 ; i < words.length ; i++) { | |
var word = words[i]; | |
var capitalizedWord = word.charAt(0).toUpperCase() + word.slice(1); | |
result += capitalizedWord; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment