Front end of the Slack clone application.

groupBy.js 3.0KB

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