pluck.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { map } from './map';
  2. /**
  3. * Maps each source value (an object) to its specified nested property.
  4. *
  5. * <span class="informal">Like {@link map}, but meant only for picking one of
  6. * the nested properties of every emitted object.</span>
  7. *
  8. * <img src="./img/pluck.png" width="100%">
  9. *
  10. * Given a list of strings describing a path to an object property, retrieves
  11. * the value of a specified nested property from all values in the source
  12. * Observable. If a property can't be resolved, it will return `undefined` for
  13. * that value.
  14. *
  15. * @example <caption>Map every click to the tagName of the clicked target element</caption>
  16. * var clicks = Rx.Observable.fromEvent(document, 'click');
  17. * var tagNames = clicks.pluck('target', 'tagName');
  18. * tagNames.subscribe(x => console.log(x));
  19. *
  20. * @see {@link map}
  21. *
  22. * @param {...string} properties The nested properties to pluck from each source
  23. * value (an object).
  24. * @return {Observable} A new Observable of property values from the source values.
  25. * @method pluck
  26. * @owner Observable
  27. */
  28. export function pluck(...properties) {
  29. const length = properties.length;
  30. if (length === 0) {
  31. throw new Error('list of properties cannot be empty.');
  32. }
  33. return (source) => map(plucker(properties, length))(source);
  34. }
  35. function plucker(props, length) {
  36. const mapper = (x) => {
  37. let currentProp = x;
  38. for (let i = 0; i < length; i++) {
  39. const p = currentProp[props[i]];
  40. if (typeof p !== 'undefined') {
  41. currentProp = p;
  42. }
  43. else {
  44. return undefined;
  45. }
  46. }
  47. return currentProp;
  48. };
  49. return mapper;
  50. }
  51. //# sourceMappingURL=pluck.js.map