Created
May 27, 2014 10:04
-
-
Save tot-ra/8f92208162310d643305 to your computer and use it in GitHub Desktop.
angularjs sprintf filter
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
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]); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{{"It can replace %s and {0} with arguments "| sprintf: "this"}}