Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

pluck.ts 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { Observable } from '../Observable';
  2. import { map } from './map';
  3. import { OperatorFunction } from '../types';
  4. /* tslint:disable:max-line-length */
  5. export function pluck<T, K1 extends keyof T>(k1: K1): OperatorFunction<T, T[K1]>;
  6. export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1]>(k1: K1, k2: K2): OperatorFunction<T, T[K1][K2]>;
  7. export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(k1: K1, k2: K2, k3: K3): OperatorFunction<T, T[K1][K2][K3]>;
  8. 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]>(k1: K1, k2: K2, k3: K3, k4: K4): OperatorFunction<T, T[K1][K2][K3][K4]>;
  9. 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], K5 extends keyof T[K1][K2][K3][K4]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): OperatorFunction<T, T[K1][K2][K3][K4][K5]>;
  10. 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], K5 extends keyof T[K1][K2][K3][K4], K6 extends keyof T[K1][K2][K3][K4][K5]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): OperatorFunction<T, T[K1][K2][K3][K4][K5][K6]>;
  11. export function pluck<T, R>(...properties: string[]): OperatorFunction<T, R>;
  12. /* tslint:enable:max-line-length */
  13. /**
  14. * Maps each source value (an object) to its specified nested property.
  15. *
  16. * <span class="informal">Like {@link map}, but meant only for picking one of
  17. * the nested properties of every emitted object.</span>
  18. *
  19. * ![](pluck.png)
  20. *
  21. * Given a list of strings describing a path to an object property, retrieves
  22. * the value of a specified nested property from all values in the source
  23. * Observable. If a property can't be resolved, it will return `undefined` for
  24. * that value.
  25. *
  26. * ## Example
  27. * Map every click to the tagName of the clicked target element
  28. * ```ts
  29. * import { fromEvent } from 'rxjs';
  30. * import { pluck } from 'rxjs/operators';
  31. *
  32. * const clicks = fromEvent(document, 'click');
  33. * const tagNames = clicks.pipe(pluck('target', 'tagName'));
  34. * tagNames.subscribe(x => console.log(x));
  35. * ```
  36. *
  37. * @see {@link map}
  38. *
  39. * @param {...string} properties The nested properties to pluck from each source
  40. * value (an object).
  41. * @return {Observable} A new Observable of property values from the source values.
  42. * @method pluck
  43. * @owner Observable
  44. */
  45. export function pluck<T, R>(...properties: string[]): OperatorFunction<T, R> {
  46. const length = properties.length;
  47. if (length === 0) {
  48. throw new Error('list of properties cannot be empty.');
  49. }
  50. return (source: Observable<T>) => map(plucker(properties, length))(source as any);
  51. }
  52. function plucker(props: string[], length: number): (x: string) => any {
  53. const mapper = (x: string) => {
  54. let currentProp = x;
  55. for (let i = 0; i < length; i++) {
  56. const p = currentProp != null ? currentProp[props[i]] : undefined;
  57. if (p !== void 0) {
  58. currentProp = p;
  59. } else {
  60. return undefined;
  61. }
  62. }
  63. return currentProp;
  64. };
  65. return mapper;
  66. }