UNPKG

5.08 kBTypeScriptView Raw
1import { Observable } from '../Observable';
2import { Subject } from '../Subject';
3import { ObservableInput, OperatorFunction, SubjectLike } from '../types';
4export interface BasicGroupByOptions<K, T> {
5 element?: undefined;
6 duration?: (grouped: GroupedObservable<K, T>) => ObservableInput<any>;
7 connector?: () => SubjectLike<T>;
8}
9export interface GroupByOptionsWithElement<K, E, T> {
10 element: (value: T) => E;
11 duration?: (grouped: GroupedObservable<K, E>) => ObservableInput<any>;
12 connector?: () => SubjectLike<E>;
13}
14export declare function groupBy<T, K>(key: (value: T) => K, options: BasicGroupByOptions<K, T>): OperatorFunction<T, GroupedObservable<K, T>>;
15export declare function groupBy<T, K, E>(key: (value: T) => K, options: GroupByOptionsWithElement<K, E, T>): OperatorFunction<T, GroupedObservable<K, E>>;
16export declare function groupBy<T, K extends T>(key: (value: T) => value is K): OperatorFunction<T, GroupedObservable<true, K> | GroupedObservable<false, Exclude<T, K>>>;
17export declare function groupBy<T, K>(key: (value: T) => K): OperatorFunction<T, GroupedObservable<K, T>>;
18/**
19 * @deprecated use the options parameter instead.
20 */
21export 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 */
25export 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, groupBy, mergeMap, reduce } from 'rxjs';
50 *
51 * of(
52 * { id: 1, name: 'JavaScript' },
53 * { id: 2, name: 'Parcel' },
54 * { id: 2, name: 'webpack' },
55 * { id: 1, name: 'TypeScript' },
56 * { id: 3, name: 'TSLint' }
57 * ).pipe(
58 * groupBy(p => p.id),
59 * mergeMap(group$ => group$.pipe(reduce((acc, cur) => [...acc, cur], [])))
60 * )
61 * .subscribe(p => console.log(p));
62 *
63 * // displays:
64 * // [{ id: 1, name: 'JavaScript' }, { id: 1, name: 'TypeScript'}]
65 * // [{ id: 2, name: 'Parcel' }, { id: 2, name: 'webpack'}]
66 * // [{ id: 3, name: 'TSLint' }]
67 * ```
68 *
69 * Pivot data on the `id` field
70 *
71 * ```ts
72 * import { of, groupBy, mergeMap, reduce, map } from 'rxjs';
73 *
74 * of(
75 * { id: 1, name: 'JavaScript' },
76 * { id: 2, name: 'Parcel' },
77 * { id: 2, name: 'webpack' },
78 * { id: 1, name: 'TypeScript' },
79 * { id: 3, name: 'TSLint' }
80 * ).pipe(
81 * groupBy(p => p.id, { element: p => p.name }),
82 * mergeMap(group$ => group$.pipe(reduce((acc, cur) => [...acc, cur], [`${ group$.key }`]))),
83 * map(arr => ({ id: parseInt(arr[0], 10), values: arr.slice(1) }))
84 * )
85 * .subscribe(p => console.log(p));
86 *
87 * // displays:
88 * // { id: 1, values: [ 'JavaScript', 'TypeScript' ] }
89 * // { id: 2, values: [ 'Parcel', 'webpack' ] }
90 * // { id: 3, values: [ 'TSLint' ] }
91 * ```
92 *
93 * @param key A function that extracts the key
94 * for each item.
95 * @param element A function that extracts the
96 * return element for each item.
97 * @param duration
98 * A function that returns an Observable to determine how long each group should
99 * exist.
100 * @param connector Factory function to create an
101 * intermediate Subject through which grouped elements are emitted.
102 * @return A function that returns an Observable that emits GroupedObservables,
103 * each of which corresponds to a unique key value and each of which emits
104 * those items from the source Observable that share that key value.
105 *
106 * @deprecated Use the options parameter instead.
107 */
108export 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>>;
109/**
110 * An observable of values that is the emitted by the result of a {@link groupBy} operator,
111 * contains a `key` property for the grouping.
112 */
113export interface GroupedObservable<K, T> extends Observable<T> {
114 /**
115 * The key value for the grouped notifications.
116 */
117 readonly key: K;
118}
119//# sourceMappingURL=groupBy.d.ts.map
\No newline at end of file