ninetails
11/18/2014 - 3:58 PM

sprintf.as

function sprintf(){
	var args = Array.prototype.slice.call(arguments), f:Array = [], str = args.shift().split("%s");
	while (str.length) {
		f.push(str.shift(), args.shift() || "");
	}
	return f.join("");
}

trace(sprintf("Hello %s, how ya %s?", "motherf*cker", "durin"));
function sprintf(){
	var args = Array.prototype.slice.call(arguments);
	return args.shift().replace(/%s/g, function(){
		return args.shift();
	});
}

alert(sprintf("Hello %s, how ya %s?", "motherf*cker", "durin"));
// http://mir.aculo.us/2011/03/09/little-helpers-a-tweet-sized-javascript-templating-engine/

function t(s,d){
  for(var p in d)
    s = s.replace(new RegExp('{'+p+'}','g'), d[p]);
  return s;
}

// t("Hello {who}!", { who: "JavaScript" }); -> "Hello JavaScript!"
function x(s, d) {
  return s.replace(/\{([\w]+)\}/ig, function(a, b){
    return d[b];
  });
}

// x("Hello {who}!", { who: "JavaScript" }); -> "Hello JavaScript!"

// http://jsperf.com/tweet-sized-template-engines