Skip to content

Instantly share code, notes, and snippets.

@ricardo-rossi
Last active Aug 29, 2015
Embed
What would you like to do?
/*
sformat(str, arg1, arg2, etc..)
USAGE:
==============================================================
sformat("{0}", "test"); //returns test
sformat("{{0}}"); //returns {0}
sformat("{{{0}}}", "test"); //returns {test}
sformat("{0} {0}", "test"); //returns test test
sformat("{0} {1}", "first", "second"); //returns first second
sformat("{1} {0}", "second", "first"); //returns first second
*/
var sformat = (function() {
var pattern = /\{\{|\}\}|\{(\d+)\}/g;
return function () {
var parameters = arguments;
return parameters[0].replace(pattern, function (match, group) {
var value;
if (match === "{{") {
return "{";
}
if (match === "}}") {
return "}";
}
value = parameters[parseInt(group, 10) + 1];
return value ? value.toString() : "";
});
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment