Skip to content

Instantly share code, notes, and snippets.

@cubicleWar
Last active October 27, 2017 03:41
Show Gist options
  • Select an option

  • Save cubicleWar/f6a444ea32b76e041f40 to your computer and use it in GitHub Desktop.

Select an option

Save cubicleWar/f6a444ea32b76e041f40 to your computer and use it in GitHub Desktop.
A function to apply a string of pipe separated filters to a given value using the angular $filter service.
//
// Applies a sequence of angular filters to a given variable
//
// @param $filter The angular filter service
// @param value The variable to apply the filters on
// @param filterSpec A pipe separated list of filters to apply to the value e.g "filter1|filter2|filerN"
//
function applyFilters($filter, value, filterSpec)
{
var filters = filterSpec.trim().split('|'),
result = value;
for (var i = 0, len = filters.length; i < len; i++)
{
var args = filters[i].trim().split(':'),
filter = $filter(args.shift().trim());
args.unshift(result);
result = filter.apply(null, args);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment