Advertisement
sohotcall

sprintf.jquery.js

Feb 12th, 2020
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ( function ( $ ){
  2.  
  3. $.sprintf = ( function (){
  4.     var args = arguments;
  5.     var str = args[0];
  6.     var mode = 1;
  7.     var index = 0;
  8.     var result = '';
  9.     var num = 0;
  10.     var precisionMode = false;
  11.     var settings;
  12.     for ( var i = 0; i < str.length; i++ ){
  13.         var chr = str[i];
  14.         if ( mode & 1 ){
  15.             if ( chr == '%' ){
  16.                 index++;
  17.                 precisionMode = false;
  18.                 settings = {
  19.                     precision: 0,
  20.                     width: 0,
  21.                     positiveHasSign: false,
  22.                     padChar: ' ',
  23.                     leftJustify: false,
  24.                     index: index
  25.                     };
  26.                 mode = 6;
  27.                 continue;
  28.             } else {
  29.                 result += chr;
  30.             }
  31.         }
  32.         if ( mode & 2 ){
  33.             if ( chr == '%' || ( 'a' <= chr && chr <= 'z' ) || ( 'A' <= chr && chr <= 'Z' )){
  34.                 if ( num )
  35.                     settings[ precisionMode ? 'precision' : 'width' ] = num;
  36.                 var value = args[ settings.index ];
  37.                 if ( chr == '%' )
  38.                     value = '%';
  39.                 else if ( chr == 'b' )
  40.                     value = parseInt( value, 10 ).toString(2);
  41.                 else if ( chr == 'c' )
  42.                     value = String.fromCharCode( value | 0 );
  43.                 else if ( chr == 'd' || chr == 'i')
  44.                     value = value |0;
  45.                 else if (chr == 'e' )
  46.                     value = settings.precision ? ( + value ).toExponential( settings.precision ) : ( + value ).toExponential();
  47.                 else if ( chr == 'f' )
  48.                     value = settings.precision ? ( + value ).toFixed( settings.precision ) : ( + value );
  49.                 else if ( chr == 'g' )
  50.                     value = settings.precision ? ( + value ).toPrecision( settings.precision ) : ( + value )
  51.                 else if ( chr == 'o' )
  52.                     value = ( value | 0 ).toString( 8 );
  53.                 else if ( chr == 's' )
  54.                     value = settings.precision ? ( value + '' ).substring( 0, settings.precision ) : value;
  55.                 else if ( chr == 'u' )
  56.                     value = ( value | 0 ) >>> 0;
  57.                 else if ( chr == 'x' )
  58.                     value = ( value | 0 ).toString( 16 );
  59.                 else if ( chr == 'X' )
  60.                     value = ( value | 0 ).toString( 16 ).toUpperCase();
  61.                 value += '';
  62.                 if ( settings.positiveHasSign && value > 0 )
  63.                     value = '+' + value;
  64.                 while ( value.length < settings.width )
  65.                     if ( settings.leftJustify )
  66.                         value += settings.padChar;
  67.                     else
  68.                         value = settings.padChar + value;
  69.                 result += value;
  70.                 mode = 1;
  71.                 continue;
  72.             } else if ( chr == '.' ){
  73.                 settings.width = num;
  74.                 num = 0;
  75.                 precisionMode = true;
  76.                 mode = 6;
  77.                 continue;
  78.             } else if ( chr == '$' ){
  79.                 settings.index = num;
  80.                 num = 0;
  81.                 mode = 6;
  82.                 continue;
  83.             }
  84.         }
  85.         if ( mode & 4 ){
  86.             if ( chr == '+' ){
  87.                 settings.positiveHasSign = true;
  88.             } else if ( chr == '-' ){
  89.                 settings.leftJustify = true;
  90.             } else if ( chr == ' ' || chr == '0'){
  91.                 settings.padChar = chr;
  92.             } else if ( '1' <= chr && chr <= '9' ){
  93.                 num = +chr;
  94.                 mode = 18;
  95.                 continue;
  96.             } else if ( chr = "'" ){
  97.                 mode = 8;
  98.                 continue;
  99.             }
  100.         }
  101.         if ( mode & 8 ){
  102.             settings.padChar = chr;
  103.             mode = 6;
  104.             continue;
  105.         }
  106.         if ( mode & 16 )
  107.             if ( '0' <= chr && chr <= '9' )
  108.                 num = 10 * num + +chr;
  109.     }
  110.     return result;
  111. } );
  112.  
  113. } )( jQuery );
  114.  
  115. /*
  116. <script src=jquery.js></script>
  117. <script src=sprintf.jquery.js></script>
  118. <script>
  119. // There are 5 monkeys in the tree.
  120. console.log( $.sprintf( 'There are %d monkeys in the %s.', 5, 'tree' ) );
  121. // The 5 contains 0 monkeys.
  122. console.log( $.sprintf( 'The %s contains %d monkeys.', 5, 'tree' ) );
  123. // The tree contains 5 monkeys.
  124. console.log( $.sprintf( 'The %2$s contains %1$d monkeys.', 5, 'tree' ) );
  125. // The tree contains 5 monkeys. That's a nice tree full of 5 monkeys.
  126. console.log( $.sprintf( 'The %2$s contains %1$d monkeys. That\'s a nice %2$s full of %1$d monkeys.', 5, 'tree' ) );
  127. //......123
  128. console.log( $.sprintf( "%'.9d", 123 ) );
  129. // 000000123
  130. console.log( $.sprintf( "%'.09d", 123 ) );
  131. // The tree contains 0005 monkeys.
  132. console.log( $.sprintf( 'The %2$s contains %1$04d monkeys.', 5, 'tree' ) );
  133. // 2020-02-12
  134. console.log( $.sprintf( '%04d-%02d-%02d', 2020, 2, 12 ) );
  135. // 123.10
  136. console.log( $.sprintf( '%01.2f', 123.1 ) );
  137. // 3.625e+8
  138. console.log( $.sprintf( '%.3e', 362525200 ) );
  139. </script>
  140. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement