Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

feat(vsprintf): Support variable-length arg list #4

Closed
wants to merge 1 commit into from

2 participants

@codeactual

No description provided.

@samsonjs
Owner

Thanks for the pull request, but now vsprintf behaves just like format (a.k.a. sprintf). The only point of vsprintf is to accept an array. Is there a use case for this behaviour that format doesn't cover?

@codeactual

That makes sense. Thanks for looking at it.

@codeactual codeactual closed this
@samsonjs
Owner

No problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 6, 2013
  1. @codeactual
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 3 deletions.
  1. +3 −0  Readme.md
  2. +3 −2 format-min.js
  3. +4 −1 format.js
  4. +11 −0 test_format.js
View
3  Readme.md
@@ -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.
View
5 format-min.js
@@ -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)})();
View
5 format.js
@@ -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) {
View
11 test_format.js
@@ -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');
Something went wrong with that request. Please try again.