erknrio
5/7/2015 - 2:05 PM

Capitalize From http://codereview.stackexchange.com/questions/77614/capitalize-the-first-character-of-all-words-even-when-following-a

// Opcion 1: Modificando el objeto String
String.prototype.capitalize = function(){
    return this.toLowerCase().replace( /\b\w/g, function (m) {
        return m.toUpperCase();
    });
};

String.prototype.capitalizeFirstWord = function(){
    return text.charAt(0).toUpperCase() + text.slice(1);
};

// Opcion 2: definiendo una propiedad del objeto String
Object.defineProperty(String.prototype, 'capitalize', {
  value() {
    return this.toLowerCase().replace( /\b\w/g, function (m) {
      return m.toUpperCase();
    });
  }
});

Object.defineProperty(String.prototype, 'capitalizeFirstWord', {
  value() {
    return text.charAt(0).toUpperCase() + text.slice(1);
  }
});

// Opcion 3: Empleando una funcion independiente
function capitalize(capitalize) {
    return capitalize.toLowerCase().replace(/\b\w/g, function(m) {
        return m.toUpperCase();
    });
}

function capitalizeFirstWord(text) {
  return text.charAt(0).toUpperCase() + text.slice(1);
}

// Uso
var cadena = "foo";
window.console.log(cadena.capitalize());
window.console.log(capitalize(cadena));