Nodejs String Capitalize capitalize()

Here you can find the source of capitalize()

Method Source Code

String.prototype.capitalize = function() {
    var t;//from  w  ww.  j a va  2s.com
    var capital = this;
    var cap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var low = 'abcdefghijklmnopqrstuvwxyz';
    for (var i=0;i<low.length;i++) {
        if (capital[0] === low[i]) {
            return t = cap[i] + capital.slice(1);
        }
        else t = capital;
    }
    return t;
}

console.log("the first character of this sentence is not a letter".capitalize());

// https://www.codewars.com/kata/capitalize-first-letter-of-a-string/discuss

Related

  1. capitalize()
    String.prototype.capitalize = function () {
        return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  2. capitalize()
    String.prototype.capitalize = function() {
        return this.replace(/(^|\s)([a-z])/g, function(m, p1, p2) {
            return p1 + p2.toUpperCase();
        });
    };
    
  3. capitalize()
    String.prototype.capitalize = function() {
      return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  4. capitalize()
    String.prototype.capitalize = function(){
        return this.replace(/^(.?)/, this.charAt(0).toUpperCase());
    
  5. capitalize()
    String.prototype.capitalize = function() {
        if (this) {
            return this.substr(0, 1).toUpperCase() + this.substr(1);
        } else {
            return '';
    };
    
  6. capitalize()
    if (typeof console === 'undefined') {
      console = {
        debug: function () {},
        dir:   function () {},
        info:  function () {},
        log:   function () {},
        warn:  function () {}
      };
    String.prototype.capitalize = function () {
      return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  7. capitalize()
    String.prototype.capitalize = function(){
        return this.toLowerCase().replace( /\b\w/g, function (m) {
            return m.toUpperCase();
        });
    };
    
  8. capitalize()
    exports.get_page_name = function(request) {
      if ('query' in request) {
        if ('page'in request.query) {
          var page = request.query.page;     
          if (page == "all") {
            return "All Pastes";
          } else if (page == "languages") {
            return "Languages";
          } else if ("tags") {
    ...
    
  9. capitalize()
    String.prototype.capitalize = function() {
      var strSplit = this.split(" ");
      var newStr = "";
      for (var i = 0; i < strSplit.length; i++) {
        if (i === 0) {
          newStr += strSplit[i].charAt(0).toUpperCase() + strSplit[i].slice(1).toLowerCase();
        } else {
          newStr += " " + strSplit[i].charAt(0).toUpperCase() + strSplit[i].slice(1).toLowerCase();
      return newStr;
    };
    module.exports = String