Nodejs Number Format Pad padLeft(base, chr)

Here you can find the source of padLeft(base, chr)

Method Source Code

/*//from  w  ww . j  a v a2 s .  co m
* This file is used to override/increment the default prototypes to provide global helper functions
*/

//used for date formatting, add 0s to the left
Number.prototype.padLeft = function (base, chr) {
    var len = (String(base || 10).length - String(this).length) + 1;
    return len > 0 ? new Array (len).join(chr || '0') + this : this;
};

//compare the equalness of 2 arrays
Array.prototype.compare = function (array) {
    // if the other array is a falsy value, return
    if (!array)
        return false;

    // compare lengths - can save a lot of time
    if (this.length != array.length)
        return false;

    for (var i = 0; i < this.length; i++) {
        // Check if we have nested arrays
        if (this[i] instanceof Array && array[i] instanceof Array) {
            // recurse into the nested arrays
            if (!this[i].compare(array[i]))
                return false;
        } else if (this[i] != array[i]) {
            // Warning - two different object instances will never be equal: {x:20} != {x:20}
            return false;
        }
    }
    return true;
};

Related

  1. pad(size)
    Number.prototype.pad = function(size) {
      var s = String(this);
      while (s.length < (size || 2)) {
        s = "0" + s;
      return s;
    
  2. pad(size)
    'use strict';
    Number.prototype.pad = function(size) {
      if(typeof(size) !== "number"){size = 2;}
      var s = String(this);
      while (s.length < size) s = "0" + s;
      return s;
    
  3. pad(size)
    Number.prototype.pad = function(size) {
      var s = String(this);
      if (typeof(size) !== "number") {
        size = 2;
      while (s.length < size) {
        s = "0" + s;
      return s;
    ...
    
  4. pad(size, decimalSize, decimalChar)
    Number.prototype.pad = function (size, decimalSize, decimalChar) {
        if (!decimalChar) decimalChar = '.';
        var str = this.toString();
        str = str.split(".");
        var result = str[0].pad("0", size ? size : 0);
        if (str.length==2){
            result += decimalChar + str[1].pad("0", decimalSize, true);
        return result;
    ...
    
  5. padLeft(base, chr)
    Number.prototype.padLeft = function (base, chr) {
        var len = (String(base || 10).length - String(this).length) + 1;
        return len > 0 ? new Array(len).join(chr || '0') + this : this;
    function formatDate(d) {
        return [(d.getFullYear()).padLeft(),
                (d.getMonth() + 1).padLeft(),
                d.getDate().padLeft()].join('-') +
            ' ' +
    ...
    
  6. padLeft(base, chr)
    Number.prototype.padLeft = function (base, chr) {
        var len = (String(base || 10).length - String(this).length) + 1;
        return len > 0 ? new Array(len).join(chr || '0') + this : this;
    };
    
  7. padLeft(base,chr)
    Number.prototype.padLeft = function(base,chr) {
      var len = (String(base || 10).length - String(this).length)+1;
      return len > 0? new Array(len).join(chr || '0')+this : this;
    };
    
  8. padLeft(max, c)
    Number.prototype.padLeft = function(max, c) {
      return this.toString().padLeft(max, c || '0');
    };
    
  9. padLeft(n, str)
    Number.prototype.padLeft = function(n, str) {
        return Array(n-String(this).length+1).join(str||'0')+this;
    };