0f2f639fd3050e6659cc864db63aec64e6106f284afc070fb95ca6a639ea5db54d6d560098c05336b6a675036c1f000f22771125e51fa350bbcf759500b720 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { map } from './map';
  2. import { OperatorFunction } from '../types';
  3. /* tslint:disable:max-line-length */
  4. /** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */
  5. export function pluck<T, K1 extends keyof T>(k1: K1): OperatorFunction<T, T[K1]>;
  6. /** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */
  7. export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1]>(k1: K1, k2: K2): OperatorFunction<T, T[K1][K2]>;
  8. /** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */
  9. export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(
  10. k1: K1,
  11. k2: K2,
  12. k3: K3
  13. ): OperatorFunction<T, T[K1][K2][K3]>;
  14. /** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */
  15. export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3]>(
  16. k1: K1,
  17. k2: K2,
  18. k3: K3,
  19. k4: K4
  20. ): OperatorFunction<T, T[K1][K2][K3][K4]>;
  21. /** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */
  22. export function pluck<
  23. T,
  24. K1 extends keyof T,
  25. K2 extends keyof T[K1],
  26. K3 extends keyof T[K1][K2],
  27. K4 extends keyof T[K1][K2][K3],
  28. K5 extends keyof T[K1][K2][K3][K4]
  29. >(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): OperatorFunction<T, T[K1][K2][K3][K4][K5]>;
  30. /** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */
  31. export function pluck<
  32. T,
  33. K1 extends keyof T,
  34. K2 extends keyof T[K1],
  35. K3 extends keyof T[K1][K2],
  36. K4 extends keyof T[K1][K2][K3],
  37. K5 extends keyof T[K1][K2][K3][K4],
  38. K6 extends keyof T[K1][K2][K3][K4][K5]
  39. >(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): OperatorFunction<T, T[K1][K2][K3][K4][K5][K6]>;
  40. /** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */
  41. export function pluck<
  42. T,
  43. K1 extends keyof T,
  44. K2 extends keyof T[K1],
  45. K3 extends keyof T[K1][K2],
  46. K4 extends keyof T[K1][K2][K3],
  47. K5 extends keyof T[K1][K2][K3][K4],
  48. K6 extends keyof T[K1][K2][K3][K4][K5]
  49. >(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, ...rest: string[]): OperatorFunction<T, unknown>;
  50. /** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */
  51. export function pluck<T>(...properties: string[]): OperatorFunction<T, unknown>;
  52. /* tslint:enable:max-line-length */
  53. /**
  54. * Maps each source value to its specified nested property.
  55. *
  56. * <span class="informal">Like {@link map}, but meant only for picking one of
  57. * the nested properties of every emitted value.</span>
  58. *
  59. * ![](pluck.png)
  60. *
  61. * Given a list of strings or numbers describing a path to a property, retrieves
  62. * the value of a specified nested property from all values in the source
  63. * Observable. If a property can't be resolved, it will return `undefined` for
  64. * that value.
  65. *
  66. * ## Example
  67. *
  68. * Map every click to the tagName of the clicked target element
  69. *
  70. * ```ts
  71. * import { fromEvent, pluck } from 'rxjs';
  72. *
  73. * const clicks = fromEvent(document, 'click');
  74. * const tagNames = clicks.pipe(pluck('target', 'tagName'));
  75. *
  76. * tagNames.subscribe(x => console.log(x));
  77. * ```
  78. *
  79. * @see {@link map}
  80. *
  81. * @param properties The nested properties to pluck from each source
  82. * value.
  83. * @return A function that returns an Observable of property values from the
  84. * source values.
  85. * @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8.
  86. */
  87. export function pluck<T, R>(...properties: Array<string | number | symbol>): OperatorFunction<T, R> {
  88. const length = properties.length;
  89. if (length === 0) {
  90. throw new Error('list of properties cannot be empty.');
  91. }
  92. return map((x) => {
  93. let currentProp: any = x;
  94. for (let i = 0; i < length; i++) {
  95. const p = currentProp?.[properties[i]];
  96. if (typeof p !== 'undefined') {
  97. currentProp = p;
  98. } else {
  99. return undefined;
  100. }
  101. }
  102. return currentProp;
  103. });
  104. }