Nodejs String Padding Left lpad(count, pad)

Here you can find the source of lpad(count, pad)

Method Source Code

/*****************************************************************************
   STRING HELPERS//from   w  w  w. j ava 2 s .  co  m

   A set of prototype modifiers for the String class that will help out with
   everyday string tasks.

   Author:    Jocko MacGregor
   Version:    0.1b
   Date:       January 20, 2014
 *****************************************************************************/


/*
Left pads a string to a specific size with a specific character.

count = The number of characters in the resulting string.
pad = (optional: default=0) The character to pad the string with.
*/
String.prototype.lpad = function(count, pad) {
   pad = pad || '0';
   str = this + '';
   return str.length >= count ? str : new Array(count - str.length + 1).join(pad) + str;      
};


/*
Left pads a string to a specific size with a specific character.

count = The number of characters in the resulting string.
pad = (optional: default=0) The character to pad the string with.
*/
String.prototype.rpad = function(count, pad) {
   pad = pad || '0';
   str = this + '';
   return str.length >= count ? str : str + new Array(count - str.length + 1).join(pad);      
};

Related

  1. lpad(character, count)
    String.prototype.lpad = function(character, count) {
        var ch = character || "0";
        var cnt = count || 2;
        var s = "";
        while (s.length < (cnt - this.length)) { s += ch; }
        s = s.substring(0, cnt-this.length);
        return s+this;
    var iterate = function(collection, iterator) {
    ...
    
  2. lpad(character, count)
    String.prototype.lpad = function(character, count) {
      var ch = character || "0";
      var cnt = count || 2;
      var s = "";
      while (s.length < (cnt - this.length)) { s += ch; }
      s = s.substring(0, cnt-this.length);
      return s+this;
    
  3. lpad(count, pad)
    String.prototype.lpad = function(count, pad) {
        pad = pad || '0';
        str = this + '';
        return str.length >= count ? str : new Array(count - str.length + 1).join(pad) + str;
    };
    
  4. lpad(l)
    String.prototype.lpad = function(l) {
      var s = this;
      while (s.length < l) { s = "0"+s; }
      return s;
    
  5. lpad(len, padstr)
    String.prototype.lpad = function(len, padstr) {
      return Array(len + 1 - this.length).join(padstr) + this;
    };
    String.prototype.rpad = function(len, padstr) {
      return this + Array(len + 1 - this.length).join(padstr);
    };
    
  6. lpad(len, s)
    String.prototype.lpad = function(len, s) {   
        var a = new Array(this);   
        var n = (len - this.length);   
        for ( var i = 0; i < n; i++) {   
            a.unshift(s);   
        return a.join("");   
    };