Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / filter.ts
1 import { Operator } from '../Operator';
2 import { Subscriber } from '../Subscriber';
3 import { Observable } from '../Observable';
4 import { OperatorFunction, MonoTypeOperatorFunction, TeardownLogic } from '../types';
5
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 */
12
13 /**
14  * Filter items emitted by the source Observable by only emitting those that
15  * satisfy a specified predicate.
16  *
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>
20  *
21  * ![](filter.png)
22  *
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`.
26  *
27  * ## Example
28  * Emit only click events whose target was a DIV element
29  * ```ts
30  * import { fromEvent } from 'rxjs';
31  * import { filter } from 'rxjs/operators';
32  *
33  * const clicks = fromEvent(document, 'click');
34  * const clicksOnDivs = clicks.pipe(filter(ev => ev.target.tagName === 'DIV'));
35  * clicksOnDivs.subscribe(x => console.log(x));
36  * ```
37  *
38  * @see {@link distinct}
39  * @see {@link distinctUntilChanged}
40  * @see {@link distinctUntilKeyChanged}
41  * @see {@link ignoreElements}
42  * @see {@link partition}
43  * @see {@link skip}
44  *
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
50  * `0`.
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.
55  * @method filter
56  * @owner Observable
57  */
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));
62   };
63 }
64
65 class FilterOperator<T> implements Operator<T, T> {
66   constructor(private predicate: (value: T, index: number) => boolean,
67               private thisArg?: any) {
68   }
69
70   call(subscriber: Subscriber<T>, source: any): TeardownLogic {
71     return source.subscribe(new FilterSubscriber(subscriber, this.predicate, this.thisArg));
72   }
73 }
74
75 /**
76  * We need this JSDoc comment for affecting ESDoc.
77  * @ignore
78  * @extends {Ignored}
79  */
80 class FilterSubscriber<T> extends Subscriber<T> {
81
82   count: number = 0;
83
84   constructor(destination: Subscriber<T>,
85               private predicate: (value: T, index: number) => boolean,
86               private thisArg: any) {
87     super(destination);
88   }
89
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) {
93     let result: any;
94     try {
95       result = this.predicate.call(this.thisArg, value, this.count++);
96     } catch (err) {
97       this.destination.error(err);
98       return;
99     }
100     if (result) {
101       this.destination.next(value);
102     }
103   }
104 }