1 import { Operator } from '../Operator';
2 import { Subscriber } from '../Subscriber';
3 import { Observable } from '../Observable';
4 import { OperatorFunction, MonoTypeOperatorFunction, TeardownLogic } from '../types';
6 /* tslint:disable:max-line-length */
7 export function filter<T, S extends T>(predicate: (value: T, index: number) => value is S,
8 thisArg?: any): OperatorFunction<T, S>;
9 export function filter<T>(predicate: (value: T, index: number) => boolean,
10 thisArg?: any): MonoTypeOperatorFunction<T>;
11 /* tslint:enable:max-line-length */
14 * Filter items emitted by the source Observable by only emitting those that
15 * satisfy a specified predicate.
17 * <span class="informal">Like
18 * [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter),
19 * it only emits a value from the source if it passes a criterion function.</span>
23 * Similar to the well-known `Array.prototype.filter` method, this operator
24 * takes values from the source Observable, passes them through a `predicate`
25 * function and only emits those values that yielded `true`.
28 * Emit only click events whose target was a DIV element
30 * import { fromEvent } from 'rxjs';
31 * import { filter } from 'rxjs/operators';
33 * const clicks = fromEvent(document, 'click');
34 * const clicksOnDivs = clicks.pipe(filter(ev => ev.target.tagName === 'DIV'));
35 * clicksOnDivs.subscribe(x => console.log(x));
38 * @see {@link distinct}
39 * @see {@link distinctUntilChanged}
40 * @see {@link distinctUntilKeyChanged}
41 * @see {@link ignoreElements}
42 * @see {@link partition}
45 * @param {function(value: T, index: number): boolean} predicate A function that
46 * evaluates each value emitted by the source Observable. If it returns `true`,
47 * the value is emitted, if `false` the value is not passed to the output
48 * Observable. The `index` parameter is the number `i` for the i-th source
49 * emission that has happened since the subscription, starting from the number
51 * @param {any} [thisArg] An optional argument to determine the value of `this`
52 * in the `predicate` function.
53 * @return {Observable} An Observable of values from the source that were
54 * allowed by the `predicate` function.
58 export function filter<T>(predicate: (value: T, index: number) => boolean,
59 thisArg?: any): MonoTypeOperatorFunction<T> {
60 return function filterOperatorFunction(source: Observable<T>): Observable<T> {
61 return source.lift(new FilterOperator(predicate, thisArg));
65 class FilterOperator<T> implements Operator<T, T> {
66 constructor(private predicate: (value: T, index: number) => boolean,
67 private thisArg?: any) {
70 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
71 return source.subscribe(new FilterSubscriber(subscriber, this.predicate, this.thisArg));
76 * We need this JSDoc comment for affecting ESDoc.
80 class FilterSubscriber<T> extends Subscriber<T> {
84 constructor(destination: Subscriber<T>,
85 private predicate: (value: T, index: number) => boolean,
86 private thisArg: any) {
90 // the try catch block below is left specifically for
91 // optimization and perf reasons. a tryCatcher is not necessary here.
92 protected _next(value: T) {
95 result = this.predicate.call(this.thisArg, value, this.count++);
97 this.destination.error(err);
101 this.destination.next(value);