zoxon
4/12/2017 - 5:02 AM

Camelize a string in Javascript. Example: camelize("hello_world") --> "HelloWorld"

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;
 
}