mirror of
https://github.com/samsonjs/format.git
synced 2026-03-25 08:45:53 +00:00
Merge 6d53f79fdc into c9f6afa190
This commit is contained in:
commit
378ec87e28
4 changed files with 21 additions and 3 deletions
|
|
@ -31,6 +31,9 @@ Usage
|
|||
vsprintf('%d is the answer to %s', [42, what])
|
||||
// => '42 is the answer to life, the universe, and everything'
|
||||
|
||||
vsprintf('%d is the answer to %s', 42, what)
|
||||
// => '42 is the answer to life, the universe, and everything'
|
||||
|
||||
Supported format specifiers: b, c, d, f, o, s, x, and X.
|
||||
|
||||
See `man 3 printf` or `man 1 printf` for details.
|
||||
|
|
|
|||
5
format-min.js
vendored
5
format-min.js
vendored
|
|
@ -1,2 +1,3 @@
|
|||
(function(){function f(){console.log(e.apply(null,arguments))}function e(h){for(var e=1,d=[].slice.call(arguments),g=0,f=h.length,a="",b,i=!1,j,c=function(){return d[e++]},k=function(){for(var a="";h[g].match(/\d/);)a+=h[g++];return a.length>0?parseInt(a):null};g<f;++g)if(b=h[g],i)switch(i=!1,j=k(),b){case "b":a+=parseInt(c(),10).toString(2);break;case "c":b=c();a+=typeof b==="string"||b instanceof String?b:String.fromCharCode(parseInt(b,10));break;case "d":a+=parseInt(c(),10);break;case "f":a+=parseFloat(c()).toFixed(j||
|
||||
6);break;case "o":a+="0"+parseInt(c(),10).toString(8);break;case "s":a+=c();break;case "x":a+="0x"+parseInt(c(),10).toString(16);break;case "X":a+="0x"+parseInt(c(),10).toString(16).toUpperCase();break;default:a+=b}else b==="%"?i=!0:a+=b;return a}var d;d=typeof module!=="undefined"?module.exports=e:function(){return this||(0,eval)("this")}();d.format=e;d.vsprintf=function(d,f){return e.apply(null,[d].concat(f))};if(typeof console!=="undefined"&&typeof console.log==="function")d.printf=f})();
|
||||
(function(){function k(){console.log(e.apply(null,arguments))}function e(h){for(var f=1,e=[].slice.call(arguments),g=0,d=h.length,a="",b,l=!1,m,c=function(){return e[f++]},k=function(){for(var a="";h[g].match(/\d/);)a+=h[g++];return 0<a.length?parseInt(a):null};g<d;++g)if(b=h[g],l)switch(l=!1,m=k(),b){case "b":a+=parseInt(c(),10).toString(2);break;case "c":b=c();a="string"===typeof b||b instanceof String?a+b:a+String.fromCharCode(parseInt(b,10));break;case "d":a+=parseInt(c(),10);break;case "f":a+=
|
||||
parseFloat(c()).toFixed(m||6);break;case "o":a+="0"+parseInt(c(),10).toString(8);break;case "s":a+=c();break;case "x":a+="0x"+parseInt(c(),10).toString(16);break;case "X":a+="0x"+parseInt(c(),10).toString(16).toUpperCase();break;default:a+=b}else"%"===b?l=!0:a+=b;return a}var d;d="undefined"!==typeof module?module.exports=e:function(){return this||(0,eval)("this")}();d.format=e;d.vsprintf=function(d,f){f instanceof Array||(f=[].slice.call(arguments,1));return e.apply(null,[d].concat(f))};"undefined"!==
|
||||
typeof console&&"function"===typeof console.log&&(d.printf=k)})();
|
||||
|
|
|
|||
|
|
@ -37,7 +37,10 @@
|
|||
}
|
||||
|
||||
function vsprintf(fmt, replacements) {
|
||||
return format.apply(null, [fmt].concat(replacements));
|
||||
if (!(replacements instanceof Array)) {
|
||||
replacements = [].slice.call(arguments, 1);
|
||||
}
|
||||
return format.apply(null, [fmt].concat(replacements))
|
||||
}
|
||||
|
||||
function format(fmt) {
|
||||
|
|
|
|||
|
|
@ -22,4 +22,15 @@ assertEqual(format.format('hello %s', 'sami'), 'hello sami');
|
|||
assertEqual(format.format('b: %b\nc: %c\nd: %d\nf: %f\no: %o\ns: %s\nx: %x\nX: %X', 42, 65, 42*42, 42*42*42/1000000000, 255, 'sami', 0xfeedface, 0xc0ffee), "b: 101010\nc: A\nd: 1764\nf: 0.000074\no: 0377\ns: sami\nx: 0xfeedface\nX: 0xC0FFEE");
|
||||
console.log('(pass)');
|
||||
|
||||
console.log('Testing vsprintf (replacements as array):');
|
||||
assertEqual(format.vsprintf('hello'), 'hello');
|
||||
assertEqual(format.vsprintf('hello %s', ['sami']), 'hello sami');
|
||||
assertEqual(format.vsprintf('b: %b\nc: %c\nd: %d\nf: %f\no: %o\ns: %s\nx: %x\nX: %X', [42, 65, 42*42, 42*42*42/1000000000, 255, 'sami', 0xfeedface, 0xc0ffee]), "b: 101010\nc: A\nd: 1764\nf: 0.000074\no: 0377\ns: sami\nx: 0xfeedface\nX: 0xC0FFEE");
|
||||
console.log('(pass)');
|
||||
|
||||
console.log('Testing vsprintf (replacements as variable arg list)');
|
||||
assertEqual(format.vsprintf('hello %s', 'sami'), 'hello sami');
|
||||
assertEqual(format.vsprintf('b: %b\nc: %c\nd: %d\nf: %f\no: %o\ns: %s\nx: %x\nX: %X', 42, 65, 42*42, 42*42*42/1000000000, 255, 'sami', 0xfeedface, 0xc0ffee), "b: 101010\nc: A\nd: 1764\nf: 0.000074\no: 0377\ns: sami\nx: 0xfeedface\nX: 0xC0FFEE");
|
||||
console.log('(pass)');
|
||||
|
||||
console.log('all passed');
|
||||
|
|
|
|||
Loading…
Reference in a new issue