Skip to content

Instantly share code, notes, and snippets.

@mgcrea
Last active August 29, 2015 14:26
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 mgcrea/556bd2e688e5c3822b1f to your computer and use it in GitHub Desktop.
Save mgcrea/556bd2e688e5c3822b1f to your computer and use it in GitHub Desktop.
Angular2 FilterPipe
import {Pipe, PipeFactory, NullPipeFactory} from 'angular2/angular2';
import {Pipes} from 'angular2/change_detection';
export class FilterPipe implements Pipe {
supports(array) {
return Array.isArray(array);
}
onDestroy() {}
transform(values, args = []) {
return !args[0] ? values : values.filter(function(object) {
return Object.keys(object).some(function(key) {
return String(object[key]).toLowerCase().indexOf(String(args[0]).toLowerCase()) !== -1;
});
});
}
}
// We create a factory since we create an instance for each binding for stateful pipes
export class FilterFactory implements PipeFactory {
supports(array): boolean {
return Array.isArray(array);
}
create(ref): Pipe {
return new FilterPipe();
}
}
// Since templates in angular are async we are passing the value to
// NullPipeFactory if the value is not supported
export var filter = [new FilterFactory(), new NullPipeFactory()];
export var filterPipe = Pipes.extend({'filter': filter});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment