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