UNPKG

6.12 kBTypeScriptView Raw
1import { Id } from "./data-interface";
2/**
3 * Data stream
4 *
5 * @remarks
6 * {@link DataStream} offers an always up to date stream of items from a {@link DataSet} or {@link DataView}.
7 * That means that the stream is evaluated at the time of iteration, conversion to another data type or when {@link cache} is called, not when the {@link DataStream} was created.
8 * Multiple invocations of for example {@link toItemArray} may yield different results (if the data source like for example {@link DataSet} gets modified).
9 * @typeParam Item - The item type this stream is going to work with.
10 */
11export declare class DataStream<Item> implements Iterable<[Id, Item]> {
12 private readonly _pairs;
13 /**
14 * Create a new data stream.
15 *
16 * @param pairs - The id, item pairs.
17 */
18 constructor(pairs: Iterable<[Id, Item]>);
19 /**
20 * Return an iterable of key, value pairs for every entry in the stream.
21 */
22 [Symbol.iterator](): IterableIterator<[Id, Item]>;
23 /**
24 * Return an iterable of key, value pairs for every entry in the stream.
25 */
26 entries(): IterableIterator<[Id, Item]>;
27 /**
28 * Return an iterable of keys in the stream.
29 */
30 keys(): IterableIterator<Id>;
31 /**
32 * Return an iterable of values in the stream.
33 */
34 values(): IterableIterator<Item>;
35 /**
36 * Return an array containing all the ids in this stream.
37 *
38 * @remarks
39 * The array may contain duplicities.
40 * @returns The array with all ids from this stream.
41 */
42 toIdArray(): Id[];
43 /**
44 * Return an array containing all the items in this stream.
45 *
46 * @remarks
47 * The array may contain duplicities.
48 * @returns The array with all items from this stream.
49 */
50 toItemArray(): Item[];
51 /**
52 * Return an array containing all the entries in this stream.
53 *
54 * @remarks
55 * The array may contain duplicities.
56 * @returns The array with all entries from this stream.
57 */
58 toEntryArray(): [Id, Item][];
59 /**
60 * Return an object map containing all the items in this stream accessible by ids.
61 *
62 * @remarks
63 * In case of duplicate ids (coerced to string so `7 == '7'`) the last encoutered appears in the returned object.
64 * @returns The object map of all id → item pairs from this stream.
65 */
66 toObjectMap(): Record<Id, Item>;
67 /**
68 * Return a map containing all the items in this stream accessible by ids.
69 *
70 * @returns The map of all id → item pairs from this stream.
71 */
72 toMap(): Map<Id, Item>;
73 /**
74 * Return a set containing all the (unique) ids in this stream.
75 *
76 * @returns The set of all ids from this stream.
77 */
78 toIdSet(): Set<Id>;
79 /**
80 * Return a set containing all the (unique) items in this stream.
81 *
82 * @returns The set of all items from this stream.
83 */
84 toItemSet(): Set<Item>;
85 /**
86 * Cache the items from this stream.
87 *
88 * @remarks
89 * This method allows for items to be fetched immediatelly and used (possibly multiple times) later.
90 * It can also be used to optimize performance as {@link DataStream} would otherwise reevaluate everything upon each iteration.
91 *
92 * ## Example
93 * ```javascript
94 * const ds = new DataSet([…])
95 *
96 * const cachedStream = ds.stream()
97 * .filter(…)
98 * .sort(…)
99 * .map(…)
100 * .cached(…) // Data are fetched, processed and cached here.
101 *
102 * ds.clear()
103 * chachedStream // Still has all the items.
104 * ```
105 * @returns A new {@link DataStream} with cached items (detached from the original {@link DataSet}).
106 */
107 cache(): DataStream<Item>;
108 /**
109 * Get the distinct values of given property.
110 *
111 * @param callback - The function that picks and possibly converts the property.
112 * @typeParam T - The type of the distinct value.
113 * @returns A set of all distinct properties.
114 */
115 distinct<T>(callback: (item: Item, id: Id) => T): Set<T>;
116 /**
117 * Filter the items of the stream.
118 *
119 * @param callback - The function that decides whether an item will be included.
120 * @returns A new data stream with the filtered items.
121 */
122 filter(callback: (item: Item, id: Id) => boolean): DataStream<Item>;
123 /**
124 * Execute a callback for each item of the stream.
125 *
126 * @param callback - The function that will be invoked for each item.
127 */
128 forEach(callback: (item: Item, id: Id) => boolean): void;
129 /**
130 * Map the items into a different type.
131 *
132 * @param callback - The function that does the conversion.
133 * @typeParam Mapped - The type of the item after mapping.
134 * @returns A new data stream with the mapped items.
135 */
136 map<Mapped>(callback: (item: Item, id: Id) => Mapped): DataStream<Mapped>;
137 /**
138 * Get the item with the maximum value of given property.
139 *
140 * @param callback - The function that picks and possibly converts the property.
141 * @returns The item with the maximum if found otherwise null.
142 */
143 max(callback: (item: Item, id: Id) => number): Item | null;
144 /**
145 * Get the item with the minimum value of given property.
146 *
147 * @param callback - The function that picks and possibly converts the property.
148 * @returns The item with the minimum if found otherwise null.
149 */
150 min(callback: (item: Item, id: Id) => number): Item | null;
151 /**
152 * Reduce the items into a single value.
153 *
154 * @param callback - The function that does the reduction.
155 * @param accumulator - The initial value of the accumulator.
156 * @typeParam T - The type of the accumulated value.
157 * @returns The reduced value.
158 */
159 reduce<T>(callback: (accumulator: T, item: Item, id: Id) => T, accumulator: T): T;
160 /**
161 * Sort the items.
162 *
163 * @param callback - Item comparator.
164 * @returns A new stream with sorted items.
165 */
166 sort(callback: (itemA: Item, itemB: Item, idA: Id, idB: Id) => number): DataStream<Item>;
167}
168//# sourceMappingURL=data-stream.d.ts.map
\No newline at end of file