Nodejs String Padding pad(side, ch, maxLength)

Here you can find the source of pad(side, ch, maxLength)

Method Source Code

String.prototype.pad = function(side, ch, maxLength) {
   var padding = "";
   /*from   w ww .  j  av  a2  s . c om*/
   for(var i = this.length; i < maxLength; i++) {
      padding += ch;
   }
   
   return (side === "left") ? (padding + this) : (this + padding);
};

Related

  1. pad(length, character)
    String.prototype.pad = function(length, character)
        if (!character)
        character = " ";
      var str = this;
        while (str.length < length)
            str += character;
        return str;
    };
    ...
    
  2. pad(length, character, direction)
    String.prototype.pad = function(length, character, direction) {
      var self = this;
      var STR_PAD_LEFT = 1;
      var STR_PAD_RIGHT = 2;
      var STR_PAD_BOTH = 3;
        if (typeof(length) == "undefined") { var length = 0; }
        if (typeof(character) == "undefined") { var character = ' '; }
        if (typeof(direction) == "undefined") { var direction = STR_PAD_RIGHT; }
        if (length + 1 >= self.length) {
    ...
    
  3. pad(n, char)
    String.prototype.pad = Number.prototype.pad = function(n, char)
      char = (typeof char !== 'undefined') ? char : 0;
      for ( var i=0,nstr = ''; (n-(this.toString()).length) > i; ++i)
        nstr += char;
      return nstr + this;
    };
    
  4. pad(n,left, str)
    String.prototype.pad = function (n,left, str) {
      var string = String(this).substr(0,n);
      var empty = Array(n-string.length+1).join(str||' ');
      return left ? empty + this : this + empty;
    
  5. pad(number, padChar)
    String.prototype.pad = function (number, padChar) {
        var toAddCount = Math.max(0, number - this.length + 1);
        padChar = padChar || '0';
        return new Array(toAddCount).join(padChar) + this;
    };
    
  6. pad(str, max)
    var pad = function (str, max) {
      str = str.toString();
      return str.length < max ? pad("0" + str, max) : str;
    
  7. pad(width, z)
    String.prototype.pad = function (width, z) {
        var n = this.substr(0);
        z = z || '0';
        var newval = n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
        return newval;
    };
    
  8. padding(val, len)
    String.prototype.padding = function (val, len) {
       var s = "";
       for (var i=0;i<len;i++)
          s+=val;
       return s;
    };