Nodejs String Capitalize toCapitalCase( allCapsWordLength )

Here you can find the source of toCapitalCase( allCapsWordLength )

Method Source Code

var ID_DELIMITER = '-';

function sanitizeString( input ) {
    //return input.replace( /\W/g, ID_DELIMITER );
    return input.split( /\W/g )[1];
}

String.prototype.toCapitalCase = function( allCapsWordLength ) {
    if ( isNaN( parseInt( allCapsWordLength ) ) ) {
        allCapsWordLength = 3;//w  ww  . j  a  va 2  s  .  c o m
    }
    
    var words = this.split(' ');
    var capitalCaseString = '';
    
    for ( var i = 0; i < words.length; i++ ) {
        var word = words[i];
        if (word.length > allCapsWordLength) {
            capitalCaseString += word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase() + ' ';
        } else {
            capitalCaseString += word.toUpperCase() + ' ';
        }
    }

    return capitalCaseString;
};

module.exports = sanitizeString;

Related

  1. capitalize_all()
    String.prototype.capitalize_all = function() {
      var words = [];
      this.split(' ').forEach(function(word) {
        words.push( word.charAt(0).toUpperCase() + word.slice(1) );
      });
      return words.join(" ");
    String.prototype.capitalize_first = function() {
      return this.charAt(0).toUpperCase() + this.slice(1);
    ...
    
  2. capitalizecapitaliseFirstLetter()
    String.prototype.capitalize = function capitaliseFirstLetter() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  3. capitalizecapitalize()
    String.prototype.capitalize = function capitalize() {
      return this[0].toUpperCase() + this.slice(1);
    };
    
  4. capitalizej()
    String.prototype.capitalize  = jCube.String.capitalize  = function () {
      return this.charAt(0).toUpperCase() + this.substring(1).replace( /\s[a-z]/g, function(s){ return s.toUpperCase(); });
    
  5. capitalizingFirstLetterOfEveryWord()
    String.prototype.capitalizingFirstLetterOfEveryWord = function () {
      return this.replace(/(?:^|\s)\S/g, function(m){ return m.toUpperCase(); });
    };
    
  6. toCapitalizeCase()
    String.prototype.toCapitalizeCase = function () {
        return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  7. ucFirst()
    String.prototype.ucFirst = function() {
        var str = this;
        if(str.length) {
            str = str.charAt(0).toUpperCase() + str.slice(1);
        return str;
    };
    
  8. ucFirst()
    String.prototype.ucFirst = function () {
      return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  9. ucFirst()
    String.prototype.ucFirst = function()
        return this.charAt(0).toUpperCase() + this.substr(1);