@@ -8,7 +8,7 @@ type SyncOrAsync<T> = T | Promise<T>
8
8
* @returns A TransformStream that applies the transformation
9
9
* @example
10
10
* ```ts
11
- * const stream = sourceStream .pipeThrough(map(x => x * 2));
11
+ * const stream = readable .pipeThrough(map(x => x * 2));
12
12
* ```
13
13
*/
14
14
export function map < T , R > (
@@ -21,6 +21,29 @@ export function map<T, R>(
21
21
} )
22
22
}
23
23
24
+ /**
25
+ * Map and flatten stream chunks
26
+ *
27
+ * @category Transformation
28
+ * @param fn - The transformation function that returns an array
29
+ * @returns A TransformStream that applies the transformation and flattens the result
30
+ * @example
31
+ * ```ts
32
+ * const stream = readable.pipeThrough(flatMap(word => word.split(''));
33
+ * ```
34
+ */
35
+ export function flatMap < T , R > (
36
+ fn : ( chunk : T ) => SyncOrAsync < R [ ] >
37
+ ) : TransformStream < T , R > {
38
+ const mapper = map < T , R [ ] > ( fn )
39
+ const flattener = flatten < R > ( )
40
+ mapper . readable . pipeThrough ( flattener )
41
+ return {
42
+ readable : flattener . readable ,
43
+ writable : mapper . writable
44
+ }
45
+ }
46
+
24
47
/**
25
48
* Filter function for filtering stream chunks
26
49
*
@@ -29,7 +52,7 @@ export function map<T, R>(
29
52
* @returns A TransformStream that only passes chunks that satisfy the predicate
30
53
* @example
31
54
* ```ts
32
- * const stream = sourceStream .pipeThrough(filter(x => x > 10));
55
+ * const stream = readable .pipeThrough(filter(x => x > 10));
33
56
* ```
34
57
*/
35
58
export function filter < T > (
@@ -52,7 +75,7 @@ export function filter<T>(
52
75
* @returns A TransformStream that passes chunks unchanged after executing the function
53
76
* @example
54
77
* ```ts
55
- * const stream = sourceStream .pipeThrough(tap(x => console.log(`Processing: ${x}`)));
78
+ * const stream = readable .pipeThrough(tap(x => console.log(`Processing: ${x}`)));
56
79
* ```
57
80
*/
58
81
export function tap < T > (
@@ -74,7 +97,7 @@ export function tap<T>(
74
97
* @returns A promise that resolves to an array of all chunks
75
98
* @example
76
99
* ```ts
77
- * const result = await toArray(sourceStream );
100
+ * const result = await toArray(readable );
78
101
* console.log(result); // [1, 2, 3, ...]
79
102
* ```
80
103
*/
@@ -98,8 +121,8 @@ export async function toArray<T>(stream: ReadableStream<T>): Promise<T[]> {
98
121
* @returns A TransformStream that groups chunks into batches
99
122
* @example
100
123
* ```ts
101
- * const stream = sourceStream .pipeThrough(batch(3));
102
- * // If sourceStream emits [1, 2, 3, 4, 5], the result will be [[1, 2, 3], [4, 5]]
124
+ * const stream = readable .pipeThrough(batch(3));
125
+ * // If readable emits [1, 2, 3, 4, 5], the result will be [[1, 2, 3], [4, 5]]
103
126
* ```
104
127
*/
105
128
export function batch < T > ( size : number ) : TransformStream < T , T [ ] > {
@@ -129,8 +152,8 @@ export function batch<T>(size: number): TransformStream<T, T[]> {
129
152
* @returns A TransformStream that flattens arrays into individual chunks
130
153
* @example
131
154
* ```ts
132
- * const stream = sourceStream .pipeThrough(flatten());
133
- * // If sourceStream emits [[1, 2], [3, 4]], the result will be [1, 2, 3, 4]
155
+ * const stream = readable .pipeThrough(flatten());
156
+ * // If readable emits [[1, 2], [3, 4]], the result will be [1, 2, 3, 4]
134
157
* ```
135
158
*/
136
159
export function flatten < T > ( ) : TransformStream < T [ ] , T > {
@@ -151,7 +174,7 @@ export function flatten<T>(): TransformStream<T[], T> {
151
174
* @returns A TransformStream that limits the number of chunks
152
175
* @example
153
176
* ```ts
154
- * const stream = sourceStream .pipeThrough(take(3));
177
+ * const stream = readable .pipeThrough(take(3));
155
178
* // Only the first 3 chunks will pass through
156
179
* ```
157
180
*/
@@ -181,7 +204,7 @@ export function take<T>(limit: number): TransformStream<T, T> {
181
204
* @returns A TransformStream that skips the specified number of chunks
182
205
* @example
183
206
* ```ts
184
- * const stream = sourceStream .pipeThrough(skip(2));
207
+ * const stream = readable .pipeThrough(skip(2));
185
208
* // The first 2 chunks will be skipped
186
209
* ```
187
210
*/
0 commit comments