12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /** PURE_IMPORTS_START .._operators_groupBy PURE_IMPORTS_END */
  2. import { groupBy as higherOrder, GroupedObservable } from '../operators/groupBy';
  3. export { GroupedObservable };
  4. /* tslint:enable:max-line-length */
  5. /**
  6. * Groups the items emitted by an Observable according to a specified criterion,
  7. * and emits these grouped items as `GroupedObservables`, one
  8. * {@link GroupedObservable} per group.
  9. *
  10. * <img src="./img/groupBy.png" width="100%">
  11. *
  12. * @example <caption>Group objects by id and return as array</caption>
  13. * Observable.of<Obj>({id: 1, name: 'aze1'},
  14. * {id: 2, name: 'sf2'},
  15. * {id: 2, name: 'dg2'},
  16. * {id: 1, name: 'erg1'},
  17. * {id: 1, name: 'df1'},
  18. * {id: 2, name: 'sfqfb2'},
  19. * {id: 3, name: 'qfs3'},
  20. * {id: 2, name: 'qsgqsfg2'}
  21. * )
  22. * .groupBy(p => p.id)
  23. * .flatMap( (group$) => group$.reduce((acc, cur) => [...acc, cur], []))
  24. * .subscribe(p => console.log(p));
  25. *
  26. * // displays:
  27. * // [ { id: 1, name: 'aze1' },
  28. * // { id: 1, name: 'erg1' },
  29. * // { id: 1, name: 'df1' } ]
  30. * //
  31. * // [ { id: 2, name: 'sf2' },
  32. * // { id: 2, name: 'dg2' },
  33. * // { id: 2, name: 'sfqfb2' },
  34. * // { id: 2, name: 'qsgqsfg2' } ]
  35. * //
  36. * // [ { id: 3, name: 'qfs3' } ]
  37. *
  38. * @example <caption>Pivot data on the id field</caption>
  39. * Observable.of<Obj>({id: 1, name: 'aze1'},
  40. * {id: 2, name: 'sf2'},
  41. * {id: 2, name: 'dg2'},
  42. * {id: 1, name: 'erg1'},
  43. * {id: 1, name: 'df1'},
  44. * {id: 2, name: 'sfqfb2'},
  45. * {id: 3, name: 'qfs1'},
  46. * {id: 2, name: 'qsgqsfg2'}
  47. * )
  48. * .groupBy(p => p.id, p => p.name)
  49. * .flatMap( (group$) => group$.reduce((acc, cur) => [...acc, cur], ["" + group$.key]))
  50. * .map(arr => ({'id': parseInt(arr[0]), 'values': arr.slice(1)}))
  51. * .subscribe(p => console.log(p));
  52. *
  53. * // displays:
  54. * // { id: 1, values: [ 'aze1', 'erg1', 'df1' ] }
  55. * // { id: 2, values: [ 'sf2', 'dg2', 'sfqfb2', 'qsgqsfg2' ] }
  56. * // { id: 3, values: [ 'qfs1' ] }
  57. *
  58. * @param {function(value: T): K} keySelector A function that extracts the key
  59. * for each item.
  60. * @param {function(value: T): R} [elementSelector] A function that extracts the
  61. * return element for each item.
  62. * @param {function(grouped: GroupedObservable<K,R>): Observable<any>} [durationSelector]
  63. * A function that returns an Observable to determine how long each group should
  64. * exist.
  65. * @return {Observable<GroupedObservable<K,R>>} An Observable that emits
  66. * GroupedObservables, each of which corresponds to a unique key value and each
  67. * of which emits those items from the source Observable that share that key
  68. * value.
  69. * @method groupBy
  70. * @owner Observable
  71. */
  72. export function groupBy(keySelector, elementSelector, durationSelector, subjectSelector) {
  73. return higherOrder(keySelector, elementSelector, durationSelector, subjectSelector)(this);
  74. }
  75. //# sourceMappingURL=groupBy.js.map