Nodejs String Padding Left padL(width, pad)

Here you can find the source of padL(width, pad)

Method Source Code

/*//from www . ja  v  a2s.  c  om
 * Copyright (c) 2014 INSciTE.  All rights reserved
 * INSciTE is on the web at: http://www.hightechkids.org
 * This code is released under GPL; see LICENSE.txt for details.
 */

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)
      return this;

   if (!pad)
      pad = " ";
   var length = width - this.length;
   if (length < 1)
      return this.substr(0, width);

   return (String.repeat(pad, length) + this).substr(0, width);
};

Related

  1. lpad(padString, length)
    String.prototype.lpad = function(padString, length) {
      var str = this;
      while(str.length < length)
        str = padString + str;
      return str;
    
  2. lpad(padString, length)
    "use strict";
    String.prototype.lpad = function(padString, length) {
      let str = this;
      while (str.length < length)
        str = padString + str;
      return str;
    };
    
  3. lpad(padString, length)
    String.prototype.lpad = function(padString, length) {
      var str = this;
        while (str.length < length){
            str = padString + str;
        return str;
    
  4. lpad(padString, length)
    String.prototype.lpad = function(padString, length) {
      var str = this;
        while (str.length < length)
            str = padString + str;
        return str;
    
  5. lpad(pad_str, length)
    String.prototype.lpad = function(pad_str, length) {
      var str = this;
      while (str.length < length) str = pad_str + str;
      return str;
    
  6. 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;
    };
    
  7. padLeft( pPadChar, pTotalLength )
    String.prototype.padLeft = function( pPadChar, pTotalLength )
      return ( pTotalLength <= this.length ) ? this :
        ( pPadChar + this ).padLeft(pPadChar, pTotalLength);
    };
    
  8. 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);
    ...
    
  9. 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;
    };
    ...