Skip to content

Instantly share code, notes, and snippets.

@emh
Created November 13, 2013 00:43
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 emh/7441569 to your computer and use it in GitHub Desktop.
Save emh/7441569 to your computer and use it in GitHub Desktop.
I wanted to use a bunch of underscore.string (http://epeli.github.io/underscore.string/) functions as filters in an AngularJS project and rather than writing separate filters for each function I came up with this.
/**
* call titleize like:
* {{ expression | _str.'titleize' }}
* call truncate like:
* {{ expression | _str:'truncate':5:'...' }}
* etc.
*/
app.filter('_str', function() {
return function(text, fn) {
var args = Array.prototype.slice.call(arguments, 2);
args.unshift(text);
return _.str[fn].apply(fn, args);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment