source: trip-planner-front/node_modules/rxjs/src/internal/operators/every.ts@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1import { Operator } from '../Operator';
2import { Observable } from '../Observable';
3import { Subscriber } from '../Subscriber';
4import { Observer, OperatorFunction } from '../types';
5
6/**
7 * Returns an Observable that emits whether or not every item of the source satisfies the condition specified.
8 *
9 * ## Example
10 * A simple example emitting true if all elements are less than 5, false otherwise
11 * ```ts
12 * import { of } from 'rxjs';
13 * import { every } from 'rxjs/operators';
14 *
15 * of(1, 2, 3, 4, 5, 6).pipe(
16 * every(x => x < 5),
17 * )
18 * .subscribe(x => console.log(x)); // -> false
19 * ```
20 *
21 * @param {function} predicate A function for determining if an item meets a specified condition.
22 * @param {any} [thisArg] Optional object to use for `this` in the callback.
23 * @return {Observable} An Observable of booleans that determines if all items of the source Observable meet the condition specified.
24 * @method every
25 * @owner Observable
26 */
27export function every<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
28 thisArg?: any): OperatorFunction<T, boolean> {
29 return (source: Observable<T>) => source.lift(new EveryOperator(predicate, thisArg, source));
30}
31
32class EveryOperator<T> implements Operator<T, boolean> {
33 constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
34 private thisArg?: any,
35 private source?: Observable<T>) {
36 }
37
38 call(observer: Subscriber<boolean>, source: any): any {
39 return source.subscribe(new EverySubscriber(observer, this.predicate, this.thisArg, this.source));
40 }
41}
42
43/**
44 * We need this JSDoc comment for affecting ESDoc.
45 * @ignore
46 * @extends {Ignored}
47 */
48class EverySubscriber<T> extends Subscriber<T> {
49 private index: number = 0;
50
51 constructor(destination: Observer<boolean>,
52 private predicate: (value: T, index: number, source: Observable<T>) => boolean,
53 private thisArg: any,
54 private source?: Observable<T>) {
55 super(destination);
56 this.thisArg = thisArg || this;
57 }
58
59 private notifyComplete(everyValueMatch: boolean): void {
60 this.destination.next(everyValueMatch);
61 this.destination.complete();
62 }
63
64 protected _next(value: T): void {
65 let result = false;
66 try {
67 result = this.predicate.call(this.thisArg, value, this.index++, this.source);
68 } catch (err) {
69 this.destination.error(err);
70 return;
71 }
72
73 if (!result) {
74 this.notifyComplete(false);
75 }
76 }
77
78 protected _complete(): void {
79 this.notifyComplete(true);
80 }
81}
Note: See TracBrowser for help on using the repository browser.