Code coverage report for src/operator/map.ts

Statements: 100% (28 / 28)      Branches: 100% (6 / 6)      Functions: 100% (7 / 7)      Lines: 100% (24 / 24)      Ignored: none     

All files » src/operator/ » map.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  1   1 1                       1 113 1   112     1 112     1 129   1   1 129   1 129 129 129     1 344 344 29   315     1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
/**
 * Similar to the well known `Array.prototype.map` function, this operator
 * applies a projection to each value and emits that projection in the returned observable
 *
 * <img src="./img/map.png" width="100%">
 *
 * @param {Function} project the function to create projection
 * @param {any} [thisArg] an optional argument to define what `this` is in the project function
 * @returns {Observable} a observable of projected values
 */
export function map<T, R>(project: (value: T, index: number) => R, thisArg?: any): Observable<R> {
  if (typeof project !== 'function') {
    throw new TypeError('argument is not a function. Are you looking for `mapTo()`?');
  }
  return this.lift(new MapOperator(project, thisArg));
}
 
class MapOperator<T, R> implements Operator<T, R> {
  constructor(private project: (value: T, index: number) => R, private thisArg: any) {
  }
 
  call(subscriber: Subscriber<R>): Subscriber<T> {
    return new MapSubscriber(subscriber, this.project, this.thisArg);
  }
}
 
class MapSubscriber<T, R> extends Subscriber<T> {
  count: number = 0;
 
  constructor(destination: Subscriber<R>,
              private project: (value: T, index: number) => R,
              private thisArg: any) {
    super(destination);
  }
 
  protected _next(x: T) {
    const result = tryCatch(this.project).call(this.thisArg || this, x, this.count++);
    if (result === errorObject) {
      this.error(errorObject.e);
    } else {
      this.destination.next(result);
    }
  }
}