Code coverage report for src/operator/every.ts

Statements: 100% (57 / 57)      Branches: 100% (16 / 16)      Functions: 100% (10 / 10)      Lines: 100% (53 / 53)      Ignored: none     

All files » src/operator/ » every.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88      1 1 1 1 1 1 1   1   20 20   20 3 3 1   2       17 3 3 3 1   2     14     1 14 14 14     1 14   1   1 14 14   1   14 14 14   14 13       1 9 9     1 24   24 1     24 24 2 22 4       1 5   1  
import {Operator} from '../Operator';
import {Observer} from '../Observer';
import {Observable} from '../Observable';
import {ScalarObservable} from '../observable/ScalarObservable';
import {ArrayObservable} from '../observable/fromArray';
import {ErrorObservable} from '../observable/throw';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import {bindCallback} from '../util/bindCallback';
 
export function every<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
                         thisArg?: any): Observable<boolean> {
  const source = this;
  let result;
 
  if (source._isScalar) {
    result = tryCatch(predicate)(source.value, 0, source);
    if (result === errorObject) {
      return new ErrorObservable(errorObject.e, source.scheduler);
    } else {
      return new ScalarObservable(result, source.scheduler);
    }
  }
 
  if (source instanceof ArrayObservable) {
    const array = (<ArrayObservable<T>>source).array;
    let result = tryCatch((array, predicate) => array.every(<any>predicate))(array, predicate);
    if (result === errorObject) {
      return new ErrorObservable(errorObject.e, source.scheduler);
    } else {
      return new ScalarObservable(result, source.scheduler);
    }
  }
  return source.lift(new EveryOperator(predicate, thisArg, source));
}
 
class EveryOperator<T, R> implements Operator<T, R> {
  constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
              private thisArg?: any,
              private source?: Observable<T>) {
  }
 
  call(observer: Subscriber<boolean>): Subscriber<T> {
    return new EverySubscriber(observer, this.predicate, this.thisArg, this.source);
  }
}
 
class EverySubscriber<T, R> extends Subscriber<T> {
  private predicate: Function = undefined;
  private index: number = 0;
 
  constructor(destination: Observer<R>,
              predicate?: (value: T, index: number, source: Observable<T>) => boolean,
              private thisArg?: any,
              private source?: Observable<T>) {
    super(destination);
 
    if (typeof predicate === 'function') {
      this.predicate = bindCallback(predicate, thisArg, 3);
    }
  }
 
  private notifyComplete(everyValueMatch: boolean): void {
    this.destination.next(everyValueMatch);
    this.destination.complete();
  }
 
  _next(value: T): void {
    const predicate = this.predicate;
 
    if (predicate === undefined) {
      this.destination.error(new TypeError('predicate must be a function'));
    }
 
    let result = tryCatch(predicate)(value, this.index++, this.source);
    if (result === errorObject) {
      this.destination.error(result.e);
    } else if (!result) {
      this.notifyComplete(false);
    }
  }
 
  _complete(): void {
    this.notifyComplete(true);
  }
}