Code coverage report for src/operator/extended/elementAt.ts

Statements: 100% (32 / 32)      Branches: 87.5% (7 / 8)      Functions: 100% (8 / 8)      Lines: 100% (26 / 26)      Ignored: none     

All files » src/operator/extended/ » elementAt.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  1 1   1 6     1   6 6 1       1 5   1   1   5 5     1 10 3 3       1 2 2 2 1   1     2   1  
import {Operator} from '../../Operator';
import {Subscriber} from '../../Subscriber';
import {ArgumentOutOfRangeError} from '../../util/ArgumentOutOfRangeError';
 
export function elementAt(index: number, defaultValue?: any) {
  return this.lift(new ElementAtOperator(index, defaultValue));
}
 
class ElementAtOperator<T, R> implements Operator<T, R> {
 
  constructor(private index: number, private defaultValue?: any) {
    if (index < 0) {
      throw new ArgumentOutOfRangeError;
    }
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new ElementAtSubscriber(subscriber, this.index, this.defaultValue);
  }
}
 
class ElementAtSubscriber<T, R> extends Subscriber<T> {
 
  constructor(destination: Subscriber<T>, private index: number, private defaultValue?: any) {
    super(destination);
  }
 
  _next(x) {
    if (this.index-- === 0) {
      this.destination.next(x);
      this.destination.complete();
    }
  }
 
  _complete() {
    const destination = this.destination;
    Eif (this.index >= 0) {
      if (typeof this.defaultValue !== 'undefined') {
        destination.next(this.defaultValue);
      } else {
        destination.error(new ArgumentOutOfRangeError);
      }
    }
    destination.complete();
  }
}