Skip to content

Instantly share code, notes, and snippets.

@draeton
Created December 5, 2011 04:47
Show Gist options
  • Save draeton/1432334 to your computer and use it in GitHub Desktop.
Save draeton/1432334 to your computer and use it in GitHub Desktop.
printf
// courtesy of http://www.reddit.com/user/itsnotlupus @
// http://bit.ly/upcWxw
function printf(msg) {
var args = Array.prototype.slice.call(arguments,1), arg;
return msg.replace(/(%[disv])/g, function(a,val) {
arg = args.shift();
if (arg !== undefined) {
switch(val.charCodeAt(1)){
case 100: return +arg; // d
case 105: return Math.round(+arg); // i
case 115: return String(arg); // s
case 118: return arg; // v
}
}
return val;
});
}
// console.log( printf("I have %d cats, 2 %s, and %i bird. %v", -3, "dogs", 1.43, window) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment