UNPKG

1.73 kBPlain TextView Raw
1import { map as higherOrderMap } from 'rxjs/operators';
2import { Observable } from 'rxjs';
3
4/**
5 * Applies a given `project` function to each value emitted by the source
6 * Observable, and emits the resulting values as an Observable.
7 *
8 * <span class="informal">Like [Array.prototype.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map),
9 * it passes each source value through a transformation function to get
10 * corresponding output values.</span>
11 *
12 * <img src="./img/map.png" width="100%">
13 *
14 * Similar to the well known `Array.prototype.map` function, this operator
15 * applies a projection to each value and emits that projection in the output
16 * Observable.
17 *
18 * @example <caption>Map every click to the clientX position of that click</caption>
19 * var clicks = Rx.Observable.fromEvent(document, 'click');
20 * var positions = clicks.map(ev => ev.clientX);
21 * positions.subscribe(x => console.log(x));
22 *
23 * @see {@link mapTo}
24 * @see {@link pluck}
25 *
26 * @param {function(value: T, index: number): R} project The function to apply
27 * to each `value` emitted by the source Observable. The `index` parameter is
28 * the number `i` for the i-th emission that has happened since the
29 * subscription, starting from the number `0`.
30 * @param {any} [thisArg] An optional argument to define what `this` is in the
31 * `project` function.
32 * @return {Observable<R>} An Observable that emits the values from the source
33 * Observable transformed by the given `project` function.
34 * @method map
35 * @owner Observable
36 */
37export function map<T, R>(this: Observable<T>, project: (value: T, index: number) => R, thisArg?: any): Observable<R> {
38 return higherOrderMap(project, thisArg)(this);
39}