Nodejs String Justify rjust(length, padding)

Here you can find the source of rjust(length, padding)

Method Source Code

// # Place all the behaviors and hooks related to the matching controller here.
// # All this logic will automatically be available in application.js.

String.prototype.rjust = function (length, padding) {
   var paddedString = this.slice();
   while (paddedString.length < length) {
      paddedString = padding + paddedString;
   }//from   w  ww  .ja  v  a 2 s  . com
   return paddedString;
};

function setDefaultInputDate (dateInputSelector) {
   var input = $(dateInputSelector);
   if (input.length > 0) {
      var localDate = new Date().toLocaleDateString().split('/');
      localDate[0] = localDate[0].rjust(2, '0');
      localDate[1] = localDate[1].rjust(2, '0');
      var formattedDate = localDate[2] + '-' + localDate[0] + '-' + localDate[1];
      input.val(formattedDate);
   }
}

Related

  1. ljust( length, char )
    String.prototype.ljust = function( length, char ) {
        var fill = [];
        while ( fill.length + this.length < length ) {
            [fill.length] = char;
        return this + fill.join('');
    
  2. ljust( width, padding )
    String.prototype.ljust = function( width, padding ) {
      padding = padding || " ";
      padding = padding.substr( 0, 1 );
      if( this.length < width )
        return this + padding.repeat( width - this.length );
      else
        return this.toString();
    
  3. rjust( length, char )
    String.prototype.rjust = function( length, char ) {
        var fill = [];
        while ( fill.length + this.length < length ) {
            fill[fill.length] = char;
        return fill.join('') + this;
    
  4. rjust( width, padding )
    String.prototype.rjust = function( width, padding ) {
      padding = padding || " ";
      padding = padding.substr( 0, 1 );
      if( this.length < width )
          return padding.repeat( width - this.length ) + this;
        else
          return this.toString();
    };
    
  5. rjust( width, padding )
    String.prototype.rjust = function( width, padding ) {
      padding = padding || " ";
      padding = padding.substr( 0, 1 );
      if( this.length < width )
        return padding.repeat( width - this.length ) + this;
      else
        return this;
    
  6. rjust(num, padding)
    String.prototype.rjust = function(num, padding){
        if(!padding) padding = ' ';
        var s = this.toString();
        if(1 != padding.length) throw new Error('Padding must be one character.');
        if(s.length >= num) return s;
        for(var i=0; i<=num-s.length; i++)
            s = padding + s;
        return s;
    };
    ...
    
  7. rjust(width, padding)
    String.prototype.rjust = function(width, padding) {
        padding = padding || " ";
        padding = padding.substr(0, 1);
        if (this.length < width) {
            return padding.repeat(width - this.length) + this;
        } else {
            return this;
    
  8. rightJustify( length, char )
    String.prototype.rightJustify = function( length, char ) {
      var fill = [];
      while ( fill.length + this.length < length ) {
        fill[fill.length] = char;
      return this + fill.join('');
    };
    
  9. rightj(n)
    String.prototype.right  = jCube.String.right  = function ( n) {
      return this.substring( this.length-n);