1 | import { Observable } from '../Observable';
|
2 | import { Subject } from '../Subject';
|
3 | import { ObservableInput, OperatorFunction, SubjectLike } from '../types';
|
4 | interface BasicGroupByOptions<K, T> {
|
5 | element?: undefined;
|
6 | duration?: (grouped: GroupedObservable<K, T>) => ObservableInput<any>;
|
7 | connector?: () => SubjectLike<T>;
|
8 | }
|
9 | interface GroupByOptionsWithElement<K, E, T> {
|
10 | element: (value: T) => E;
|
11 | duration?: (grouped: GroupedObservable<K, E>) => ObservableInput<any>;
|
12 | connector?: () => SubjectLike<E>;
|
13 | }
|
14 | export declare function groupBy<T, K>(key: (value: T) => K, options: BasicGroupByOptions<K, T>): OperatorFunction<T, GroupedObservable<K, T>>;
|
15 | export declare function groupBy<T, K, E>(key: (value: T) => K, options: GroupByOptionsWithElement<K, E, T>): OperatorFunction<T, GroupedObservable<K, E>>;
|
16 | export declare function groupBy<T, K extends T>(key: (value: T) => value is K): OperatorFunction<T, GroupedObservable<true, K> | GroupedObservable<false, Exclude<T, K>>>;
|
17 | export declare function groupBy<T, K>(key: (value: T) => K): OperatorFunction<T, GroupedObservable<K, T>>;
|
18 | /**
|
19 | * @deprecated use the options parameter instead.
|
20 | */
|
21 | export declare function groupBy<T, K>(key: (value: T) => K, element: void, duration: (grouped: GroupedObservable<K, T>) => Observable<any>): OperatorFunction<T, GroupedObservable<K, T>>;
|
22 | /**
|
23 | * @deprecated use the options parameter instead.
|
24 | */
|
25 | export declare function groupBy<T, K, R>(key: (value: T) => K, element?: (value: T) => R, duration?: (grouped: GroupedObservable<K, R>) => Observable<any>): OperatorFunction<T, GroupedObservable<K, R>>;
|
26 | /**
|
27 | * Groups the items emitted by an Observable according to a specified criterion,
|
28 | * and emits these grouped items as `GroupedObservables`, one
|
29 | * {@link GroupedObservable} per group.
|
30 | *
|
31 | * ![](groupBy.png)
|
32 | *
|
33 | * When the Observable emits an item, a key is computed for this item with the key function.
|
34 | *
|
35 | * If a {@link GroupedObservable} for this key exists, this {@link GroupedObservable} emits. Otherwise, a new
|
36 | * {@link GroupedObservable} for this key is created and emits.
|
37 | *
|
38 | * A {@link GroupedObservable} represents values belonging to the same group represented by a common key. The common
|
39 | * key is available as the `key` field of a {@link GroupedObservable} instance.
|
40 | *
|
41 | * The elements emitted by {@link GroupedObservable}s are by default the items emitted by the Observable, or elements
|
42 | * returned by the element function.
|
43 | *
|
44 | * ## Examples
|
45 | *
|
46 | * ### Group objects by id and return as array
|
47 | *
|
48 | * ```ts
|
49 | * import { of } from 'rxjs';
|
50 | * import { mergeMap, groupBy, reduce } from 'rxjs/operators';
|
51 | *
|
52 | * of(
|
53 | * {id: 1, name: 'JavaScript'},
|
54 | * {id: 2, name: 'Parcel'},
|
55 | * {id: 2, name: 'webpack'},
|
56 | * {id: 1, name: 'TypeScript'},
|
57 | * {id: 3, name: 'TSLint'}
|
58 | * ).pipe(
|
59 | * groupBy(p => p.id),
|
60 | * mergeMap((group$) => group$.pipe(reduce((acc, cur) => [...acc, cur], [])))
|
61 | * )
|
62 | * .subscribe(p => console.log(p));
|
63 | *
|
64 | * // displays:
|
65 | * // [ { id: 1, name: 'JavaScript'},
|
66 | * // { id: 1, name: 'TypeScript'} ]
|
67 | * //
|
68 | * // [ { id: 2, name: 'Parcel'},
|
69 | * // { id: 2, name: 'webpack'} ]
|
70 | * //
|
71 | * // [ { id: 3, name: 'TSLint'} ]
|
72 | * ```
|
73 | *
|
74 | * ### Pivot data on the id field
|
75 | *
|
76 | * ```ts
|
77 | * import { of } from 'rxjs';
|
78 | * import { groupBy, map, mergeMap, reduce } from 'rxjs/operators';
|
79 | *
|
80 | * of(
|
81 | * { id: 1, name: 'JavaScript' },
|
82 | * { id: 2, name: 'Parcel' },
|
83 | * { id: 2, name: 'webpack' },
|
84 | * { id: 1, name: 'TypeScript' },
|
85 | * { id: 3, name: 'TSLint' }
|
86 | * )
|
87 | * .pipe(
|
88 | * groupBy(p => p.id, { element: p => p.name }),
|
89 | * mergeMap(group$ =>
|
90 | * group$.pipe(reduce((acc, cur) => [...acc, cur], [`${group$.key}`]))
|
91 | * ),
|
92 | * map(arr => ({ id: parseInt(arr[0], 10), values: arr.slice(1) }))
|
93 | * )
|
94 | * .subscribe(p => console.log(p));
|
95 | *
|
96 | * // displays:
|
97 | * // { id: 1, values: [ 'JavaScript', 'TypeScript' ] }
|
98 | * // { id: 2, values: [ 'Parcel', 'webpack' ] }
|
99 | * // { id: 3, values: [ 'TSLint' ] }
|
100 | * ```
|
101 | *
|
102 | * @param key A function that extracts the key
|
103 | * for each item.
|
104 | * @param element A function that extracts the
|
105 | * return element for each item.
|
106 | * @param duration
|
107 | * A function that returns an Observable to determine how long each group should
|
108 | * exist.
|
109 | * @param connector Factory function to create an
|
110 | * intermediate Subject through which grouped elements are emitted.
|
111 | * @return A function that returns an Observable that emits GroupedObservables,
|
112 | * each of which corresponds to a unique key value and each of which emits
|
113 | * those items from the source Observable that share that key value.
|
114 | *
|
115 | * @deprecated Use the options parameter instead.
|
116 | */
|
117 | export declare function groupBy<T, K, R>(key: (value: T) => K, element?: (value: T) => R, duration?: (grouped: GroupedObservable<K, R>) => Observable<any>, connector?: () => Subject<R>): OperatorFunction<T, GroupedObservable<K, R>>;
|
118 | /**
|
119 | * An observable of values that is the emitted by the result of a {@link groupBy} operator,
|
120 | * contains a `key` property for the grouping.
|
121 | */
|
122 | export interface GroupedObservable<K, T> extends Observable<T> {
|
123 | /**
|
124 | * The key value for the grouped notifications.
|
125 | */
|
126 | readonly key: K;
|
127 | }
|
128 | export {};
|
129 | //# sourceMappingURL=groupBy.d.ts.map |
\ | No newline at end of file |