Nodejs String Padding Left padLeft(count, character)

Here you can find the source of padLeft(count, character)

Method Source Code

String.prototype.padLeft  = function(count, character) {
    var result = "";

    if (character === undefined) {
        character = " ";
    }/*from  ww w.  j a v a2  s  .  c  om*/

    count -= this.length;

    if (count < 0) {
        count = 0;
    }

    var i;
    for (i = 0; i < count; i += 1) {
        result += character;
    }

    result += this;

    return result;
};

Related

  1. padLeft(char, length)
    String.prototype.padLeft = function (char, length) {
        var len = this.length;
        if (len === 0) return '';
        var str = this;
        while (len < length) {
            str = char + str;
            len++;
        return str;
    ...
    
  2. padLeft(character, length)
    'use strict';
    String.prototype.padLeft = function (character, length)  {
      return character.repeat(Math.max(0, length - this.length)) + this;
    };
    
  3. padLeft(character, length)
    'use strict';
    String.prototype.padLeft = String.prototype.padLeft || function (character, length) {
      return character.repeat(Math.max(0, length - this.length)) + this;
    };
    
  4. padLeft(count, ch)
    String.prototype.padLeft = function(count, ch) {
         var char = ch || ' ';
         var result = new Array(count + 1).join(char);
         return result + this;
     };
    
  5. padLeft(count, character)
    String.prototype.padLeft = function (count, character) {
        if (typeof (character) === "undefined") character = " ";
        return this.length <= count ? Array(count + 1 - this.length).join(character) + this : this.toString();
    
  6. padLeft(count, character)
    String.prototype.padLeft = function (count, character) {
        if (typeof (character) === "undefined") {
            character = " ";
        return this.length <= count ? Array(count + 1 - this.length).join(character) + this : this.toString();
    };
    
  7. padLeft(count, character)
    String.prototype.padLeft = function(count, character) {
      var ch = character || ' ';
      var result = '';
      for (var i = 0; i < count; i++) {
        result += ch;
      };
      result += this;
      return result;
    
  8. padLeft(count, character)
    String.prototype.padLeft = function(count, character) {
        character = character || ' ';
        return character.repeat(count) + this.toString();
    };
    
  9. padLeft(length, char)
    String.prototype.padLeft = function(length, char) {
      return Array(length - this.length + 1).join(char || " ") + this;
    };