Nodejs String Padding Left padLeft(ch, n)

Here you can find the source of padLeft(ch, n)

Method Source Code

String.prototype.padLeft = function(ch, n) {
  if(this.length < n) {
    var diff = n - this.length;
    var tempArr = this.split('');
    var tmp;/*from w  ww  .ja  v a2  s.c  o m*/
    for(var i = 0; i < diff; i++) {
      tempArr.unshift(ch);
    }
    tmp = tempArr.toString().replace(/,/g, '');
    return tmp;
  }  else if(parseInt(this).toString() !== 'NaN'){
    return parseInt(this);
  } else {
    return this;
  }
};

Related

  1. padL(width, pad)
    String.repeat = function(chr, count) {
      var str = "";
      for (var x = 0; x < count; x++) {
        str += chr;
      return str;
    };
    String.prototype.padL = function(width, pad) {
      if (!width || width < 1)
    ...
    
  2. padLeft( length, padding )
    Math.TO_DEGREES = 180 / Math.PI;
    Math.TO_RADIANS = Math.PI / 180;
    String.prototype.padLeft = function( length, padding )
        return Array( length - this.length + 1 ).join( padding || '0' ) + this;
    };
    
  3. padLeft( pPadChar, pTotalLength )
    String.prototype.padLeft = function( pPadChar, pTotalLength )
      return ( pTotalLength <= this.length ) ? this :
        ( pPadChar + this ).padLeft(pPadChar, pTotalLength);
    };
    
  4. padLeft(c, length)
    String.prototype.padLeft = function(c, length) {
        var str = this;
        while (str.length < length)
            str = c + str;
        return str;
    function formatDuration (duration) {
        var totalSeconds = Math.floor(duration);
        var minutes = Math.floor(totalSeconds / 60);
    ...
    
  5. padLeft(ch, n)
    'use strict';
    String.prototype.padLeft = function(ch, n) {
      let myString = '';
      while(myString.length < n - this.length) {
        myString += ch;
      myString += this;
      return myString;
    };
    ...
    
  6. padLeft(char, length)
    String.prototype.padLeft = function (char, length) {
        var len = this.length;
        if (len === 0) return '';
        var str = this;
        while (len < length) {
            str = char + str;
            len++;
        return str;
    ...
    
  7. padLeft(character, length)
    'use strict';
    String.prototype.padLeft = function (character, length)  {
      return character.repeat(Math.max(0, length - this.length)) + this;
    };
    
  8. padLeft(character, length)
    'use strict';
    String.prototype.padLeft = String.prototype.padLeft || function (character, length) {
      return character.repeat(Math.max(0, length - this.length)) + this;
    };
    
  9. padLeft(count, ch)
    String.prototype.padLeft = function(count, ch) {
         var char = ch || ' ';
         var result = new Array(count + 1).join(char);
         return result + this;
     };