Skip to content

Instantly share code, notes, and snippets.

@hernan
Forked from jpetitcolas/camelize.js
Created March 16, 2018 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hernan/e46fecafa6b108fd19bb245772af5090 to your computer and use it in GitHub Desktop.
Save hernan/e46fecafa6b108fd19bb245772af5090 to your computer and use it in GitHub Desktop.
Camelize a string in Javascript. Example: camelize("hello_world") --> "HelloWorld"
/**
* 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