Skip to content

Instantly share code, notes, and snippets.

@tot-ra
Created May 27, 2014 10: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 tot-ra/8f92208162310d643305 to your computer and use it in GitHub Desktop.
Save tot-ra/8f92208162310d643305 to your computer and use it in GitHub Desktop.
angularjs sprintf filter
app.filter['sprintf'] = function () {
return function (str, number) {
function parse(str) {
var args = [].slice.call(arguments, 1),
i = 0;
str = str.replace(/{(\d+)}/g, function (a, number) {
return typeof args[number] != 'undefined'
? args[number]
: str;
});
return str.replace(/%s/g, function() {
return args[i++];
});
}
return parse(str, arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
}
};
@tot-ra
Copy link
Author

tot-ra commented May 27, 2014

{{"It can replace %s and {0} with arguments "| sprintf: "this"}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment