Skip to content

Instantly share code, notes, and snippets.

@ricardo-rossi
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardo-rossi/97768f0222102df3828a to your computer and use it in GitHub Desktop.
Save ricardo-rossi/97768f0222102df3828a to your computer and use it in GitHub Desktop.
/*
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