Skip to content

Instantly share code, notes, and snippets.

@pthrasher
Created February 5, 2012 04:47
Show Gist options
  • Select an option

  • Save pthrasher/1742857 to your computer and use it in GitHub Desktop.

Select an option

Save pthrasher/1742857 to your computer and use it in GitHub Desktop.
just a short, fast string templater.
sprintf_re = /%\({1}([a-zA-Z0-9_]+)\){1}/
sprintf = (str, obj) ->
((if obj.hasOwnProperty(a) then obj[a] else a) for a in str.split sprintf_re).join("")
console.log(sprintf("%(quantity) @ %(weight)", {quantity: 13, weight: 42}))
var sprintf, sprintf_re;
sprintf_re = /%\({1}([a-zA-Z0-9_]+)\){1}/;
sprintf = function(str, obj) {
var a;
return ((function() {
var _i, _len, _ref, _results;
_ref = str.split(sprintf_re);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
a = _ref[_i];
_results.push(obj.hasOwnProperty(a) ? obj[a] : a);
}
return _results;
})()).join("");
};
console.log(sprintf("%(quantity) @ %(weight)", {
quantity: 13,
weight: 42
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment