Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ( function ( $ ){
- $.sprintf = ( function (){
- var args = arguments;
- var str = args[0];
- var mode = 1;
- var index = 0;
- var result = '';
- var num = 0;
- var precisionMode = false;
- var settings;
- for ( var i = 0; i < str.length; i++ ){
- var chr = str[i];
- if ( mode & 1 ){
- if ( chr == '%' ){
- index++;
- precisionMode = false;
- settings = {
- precision: 0,
- width: 0,
- positiveHasSign: false,
- padChar: ' ',
- leftJustify: false,
- index: index
- };
- mode = 6;
- continue;
- } else {
- result += chr;
- }
- }
- if ( mode & 2 ){
- if ( chr == '%' || ( 'a' <= chr && chr <= 'z' ) || ( 'A' <= chr && chr <= 'Z' )){
- if ( num )
- settings[ precisionMode ? 'precision' : 'width' ] = num;
- var value = args[ settings.index ];
- if ( chr == '%' )
- value = '%';
- else if ( chr == 'b' )
- value = parseInt( value, 10 ).toString(2);
- else if ( chr == 'c' )
- value = String.fromCharCode( value | 0 );
- else if ( chr == 'd' || chr == 'i')
- value = value |0;
- else if (chr == 'e' )
- value = settings.precision ? ( + value ).toExponential( settings.precision ) : ( + value ).toExponential();
- else if ( chr == 'f' )
- value = settings.precision ? ( + value ).toFixed( settings.precision ) : ( + value );
- else if ( chr == 'g' )
- value = settings.precision ? ( + value ).toPrecision( settings.precision ) : ( + value )
- else if ( chr == 'o' )
- value = ( value | 0 ).toString( 8 );
- else if ( chr == 's' )
- value = settings.precision ? ( value + '' ).substring( 0, settings.precision ) : value;
- else if ( chr == 'u' )
- value = ( value | 0 ) >>> 0;
- else if ( chr == 'x' )
- value = ( value | 0 ).toString( 16 );
- else if ( chr == 'X' )
- value = ( value | 0 ).toString( 16 ).toUpperCase();
- value += '';
- if ( settings.positiveHasSign && value > 0 )
- value = '+' + value;
- while ( value.length < settings.width )
- if ( settings.leftJustify )
- value += settings.padChar;
- else
- value = settings.padChar + value;
- result += value;
- mode = 1;
- continue;
- } else if ( chr == '.' ){
- settings.width = num;
- num = 0;
- precisionMode = true;
- mode = 6;
- continue;
- } else if ( chr == '$' ){
- settings.index = num;
- num = 0;
- mode = 6;
- continue;
- }
- }
- if ( mode & 4 ){
- if ( chr == '+' ){
- settings.positiveHasSign = true;
- } else if ( chr == '-' ){
- settings.leftJustify = true;
- } else if ( chr == ' ' || chr == '0'){
- settings.padChar = chr;
- } else if ( '1' <= chr && chr <= '9' ){
- num = +chr;
- mode = 18;
- continue;
- } else if ( chr = "'" ){
- mode = 8;
- continue;
- }
- }
- if ( mode & 8 ){
- settings.padChar = chr;
- mode = 6;
- continue;
- }
- if ( mode & 16 )
- if ( '0' <= chr && chr <= '9' )
- num = 10 * num + +chr;
- }
- return result;
- } );
- } )( jQuery );
- /*
- <script src=jquery.js></script>
- <script src=sprintf.jquery.js></script>
- <script>
- // There are 5 monkeys in the tree.
- console.log( $.sprintf( 'There are %d monkeys in the %s.', 5, 'tree' ) );
- // The 5 contains 0 monkeys.
- console.log( $.sprintf( 'The %s contains %d monkeys.', 5, 'tree' ) );
- // The tree contains 5 monkeys.
- console.log( $.sprintf( 'The %2$s contains %1$d monkeys.', 5, 'tree' ) );
- // The tree contains 5 monkeys. That's a nice tree full of 5 monkeys.
- console.log( $.sprintf( 'The %2$s contains %1$d monkeys. That\'s a nice %2$s full of %1$d monkeys.', 5, 'tree' ) );
- //......123
- console.log( $.sprintf( "%'.9d", 123 ) );
- // 000000123
- console.log( $.sprintf( "%'.09d", 123 ) );
- // The tree contains 0005 monkeys.
- console.log( $.sprintf( 'The %2$s contains %1$04d monkeys.', 5, 'tree' ) );
- // 2020-02-12
- console.log( $.sprintf( '%04d-%02d-%02d', 2020, 2, 12 ) );
- // 123.10
- console.log( $.sprintf( '%01.2f', 123.1 ) );
- // 3.625e+8
- console.log( $.sprintf( '%.3e', 362525200 ) );
- </script>
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement