646f6183a0d71e207bc1464323c2fd0ca3f3db40f616c89afd2e452095e931b0f4fae6c6ae49505aca84b51de2e3a1af7e168ec44d404e42dc28f6a894c08a 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { OperatorFunction } from '../types';
  2. import { operate } from '../util/lift';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. export function map<T, R>(project: (value: T, index: number) => R): OperatorFunction<T, R>;
  5. /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
  6. export function map<T, R, A>(project: (this: A, value: T, index: number) => R, thisArg: A): OperatorFunction<T, R>;
  7. /**
  8. * Applies a given `project` function to each value emitted by the source
  9. * Observable, and emits the resulting values as an Observable.
  10. *
  11. * <span class="informal">Like [Array.prototype.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map),
  12. * it passes each source value through a transformation function to get
  13. * corresponding output values.</span>
  14. *
  15. * ![](map.png)
  16. *
  17. * Similar to the well known `Array.prototype.map` function, this operator
  18. * applies a projection to each value and emits that projection in the output
  19. * Observable.
  20. *
  21. * ## Example
  22. *
  23. * Map every click to the `clientX` position of that click
  24. *
  25. * ```ts
  26. * import { fromEvent, map } from 'rxjs';
  27. *
  28. * const clicks = fromEvent<PointerEvent>(document, 'click');
  29. * const positions = clicks.pipe(map(ev => ev.clientX));
  30. *
  31. * positions.subscribe(x => console.log(x));
  32. * ```
  33. *
  34. * @see {@link mapTo}
  35. * @see {@link pluck}
  36. *
  37. * @param project The function to apply to each `value` emitted by the source
  38. * Observable. The `index` parameter is the number `i` for the i-th emission
  39. * that has happened since the subscription, starting from the number `0`.
  40. * @param thisArg An optional argument to define what `this` is in the
  41. * `project` function.
  42. * @return A function that returns an Observable that emits the values from the
  43. * source Observable transformed by the given `project` function.
  44. */
  45. export function map<T, R>(project: (value: T, index: number) => R, thisArg?: any): OperatorFunction<T, R> {
  46. return operate((source, subscriber) => {
  47. // The index of the value from the source. Used with projection.
  48. let index = 0;
  49. // Subscribe to the source, all errors and completions are sent along
  50. // to the consumer.
  51. source.subscribe(
  52. createOperatorSubscriber(subscriber, (value: T) => {
  53. // Call the projection function with the appropriate this context,
  54. // and send the resulting value to the consumer.
  55. subscriber.next(project.call(thisArg, value, index++));
  56. })
  57. );
  58. });
  59. }