Nodejs String Padding Left lpad(character, count)

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

Method Source Code

/**//from   ww w  .j a  v  a 2 s. com
 * Left pad
 *   ie.   "k".lpad("o",5)  --> "ooook"
 * @param {string} [character="0"]
 * @param {int} [count=2]
 */
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) {
    for (var i=0; i<collection.length; i++) {
        iterator(collection[i]);
    }
}

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;
    
  2. 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;    
    };
    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);    
    ...
    
  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;