Nodejs String Padding pad(length, character, direction)

Here you can find the source of pad(length, character, direction)

Method Source Code

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) {
        switch (direction){
            case STR_PAD_LEFT:// w  ww . j a v  a2 s . co m
                self = Array(length + 1 - self.length).join(character) + self;
               break;
            case STR_PAD_BOTH:
                var right = Math.ceil((padlen = length - self.length) / 2);
                var left = padlen - right;
                self = Array(left+1).join(character) + self + Array(right+1).join(character);
               break;
            default:
                self = self + Array(length + 1 - self.length).join(character);
               break;
        }
    }
    return self;
};

Related

  1. pad(l, s, t)
    String.prototype.pad = function(l, s, t){
        return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
            + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
            + this + s.substr(0, l - t) : this;
    };
    
  2. pad(len, character)
    document.addEventListener("DOMContentLoaded", update);
    String.prototype.pad= function(len, character){
        var s = '';
        var character = character || '0';
        var len = (len || 2)-this.length;
        return Array(len).join(character)+this;
    Number.prototype.pad = function(len, character){
      return String(this).pad(len,character);
    ...
    
  3. pad(len, str)
    String.prototype.pad = function(len, str){
        var output = this.valueOf();
        while(output.length < len){
            output = str + output;
        return output;
    };
    
  4. pad(length)
    String.prototype.pad = function(length) {
      var str = this.toString();
      while (str.length < length) {
        str = '0' + str;
      return str;
    
  5. pad(length, character)
    String.prototype.pad = function(length, character)
        if (!character)
        character = " ";
      var str = this;
        while (str.length < length)
            str += character;
        return str;
    };
    ...
    
  6. 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;
    };
    
  7. 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;
    
  8. 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;
    };
    
  9. pad(side, ch, maxLength)
    String.prototype.pad = function(side, ch, maxLength) {
      var padding = "";
      for(var i = this.length; i < maxLength; i++) {
        padding += ch;
      return (side === "left") ? (padding + this) : (this + padding);
    };