Code coverage report for src/operator/first.ts

Statements: 100% (53 / 53)      Branches: 93.75% (15 / 16)      Functions: 100% (8 / 8)      Lines: 100% (50 / 50)      Ignored: none     

All files » src/operator/ » first.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    1 1 1 1               1     17     1 17 17 17 17     1 17   1   1 17 17   1 17 17 17 17 17     1 27 27 27 27 25 25 1 1     26 7 2 2 1 1   1   5   6 6       1 4 4 2 2 2 2     1  
import {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import {EmptyError} from '../util/EmptyError';
 
/**
 * Returns an Observable that emits the first item of the source Observable that matches the specified condition.
 * Throws an error if matching element is not found.
 * @param {function} predicate function called with each item to test for condition matching.
 * @returns {Observable} an Observable of the first item that matches the condition.
 */
export function first<T, R>(predicate?: (value: T, index: number, source: Observable<T>) => boolean,
                            resultSelector?: (value: T, index: number) => R,
                            defaultValue?: R): Observable<T> | Observable<R> {
  return this.lift(new FirstOperator(predicate, resultSelector, defaultValue, this));
}
 
class FirstOperator<T, R> implements Operator<T, R> {
  constructor(private predicate?: (value: T, index: number, source: Observable<T>) => boolean,
              private resultSelector?: (value: T, index: number) => R,
              private defaultValue?: any,
              private source?: Observable<T>) {
  }
 
  call(observer: Subscriber<R>): Subscriber<T> {
    return new FirstSubscriber(observer, this.predicate, this.resultSelector, this.defaultValue, this.source);
  }
}
 
class FirstSubscriber<T, R> extends Subscriber<T> {
  private index: number = 0;
  private hasCompleted: boolean = false;
 
  constructor(destination: Subscriber<R>,
              private predicate?: (value: T, index: number, source: Observable<T>) => boolean,
              private resultSelector?: (value: T, index: number) => R,
              private defaultValue?: any,
              private source?: Observable<T>) {
    super(destination);
  }
 
  protected _next(value: T): void {
    const { destination, predicate, resultSelector } = this;
    const index = this.index++;
    let passed: any = true;
    if (predicate) {
      passed = tryCatch(predicate)(value, index, this.source);
      if (passed === errorObject) {
        destination.error(errorObject.e);
        return;
      }
    }
    if (passed) {
      if (resultSelector) {
        let result = tryCatch(resultSelector)(value, index);
        if (result === errorObject) {
          destination.error(errorObject.e);
          return;
        }
        destination.next(result);
      } else {
        destination.next(value);
      }
      destination.complete();
      this.hasCompleted = true;
    }
  }
 
  protected _complete(): void {
    const destination = this.destination;
    if (!this.hasCompleted && typeof this.defaultValue !== 'undefined') {
      destination.next(this.defaultValue);
      destination.complete();
    } else Eif (!this.hasCompleted) {
      destination.error(new EmptyError);
    }
  }
}