Nodejs String Padding pad($char, $length)

Here you can find the source of pad($char, $length)

Method Source Code

String.prototype.pad = function($char, $length) {
    var $result = this;
    //from  ww  w .j  a  v a  2 s.  c om
    while ($result.length < $length) {
        $result = $char + $result;
    }
    
    return $result;
}

Related

  1. pad( digits )
    String.prototype.pad = function( digits ) {
        var x = this;
        while ( x.length < digits ) {
            x = '0' + x;
        return x;
    };
    
  2. pad(l, r)
    String.prototype.pad = function(l, r) {
      if (r === undefined) {
        r = l
        if (!(this.substr(0, 2) == "0x" || /^\d+$/.test(this)))
          l = 0
      var ret = this.bin();
      while (ret.length < l)
        ret = "\0" + ret
    ...
    
  3. pad(l, r)
    String.prototype.pad = function(l, r) {
      if (r === undefined) {
        r = l
        if (!(this.substr(0, 2) == "0x" || /^\d+$/.test(this)))
          l = 0
      var ret = this.bin();
      while (ret.length < l)
        ret = "\0" + ret
    ...
    
  4. 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;
    };
    
  5. 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;
    };