Skip to content

Instantly share code, notes, and snippets.

@hotchpotch
Created October 19, 2009 10:47
Show Gist options
  • Save hotchpotch/213270 to your computer and use it in GitHub Desktop.
Save hotchpotch/213270 to your computer and use it in GitHub Desktop.
var sprintf = function (str) {
var args = Array.prototype.slice.call(arguments, 1);
return str.replace(/%0(\d+)d/g, function(m, num) {
var r = String(args.shift());
var c = '';
num = parseInt(num) - r.length;
while (--num >= 0) c += '0';
return c + r;
}).replace(/%[sdf]/g, function(m) { return sprintf._SPRINTF_HASH[m](args.shift()) });
};
sprintf._SPRINTF_HASH = {
'%s': String,
'%d': parseInt,
'%f': parseFloat
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment