Last active
August 29, 2015 14:04
-
-
Save ricardo-rossi/97768f0222102df3828a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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