Created
December 5, 2011 04:47
-
-
Save draeton/1432334 to your computer and use it in GitHub Desktop.
printf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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