@soffinal/stream 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -6
- package/dist/index.js.map +14 -1
- package/dist/list.d.ts +3 -3
- package/dist/list.d.ts.map +1 -1
- package/dist/map.d.ts +1 -1
- package/dist/map.d.ts.map +1 -1
- package/dist/set.d.ts +1 -1
- package/dist/set.d.ts.map +1 -1
- package/dist/state.d.ts +1 -1
- package/dist/state.d.ts.map +1 -1
- package/dist/stream.d.ts +4 -0
- package/dist/stream.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/benchmark.js +0 -151
- package/dist/benchmark.js.map +0 -1
- package/dist/list.js +0 -200
- package/dist/list.js.map +0 -1
- package/dist/map.js +0 -99
- package/dist/map.js.map +0 -1
- package/dist/set.js +0 -94
- package/dist/set.js.map +0 -1
- package/dist/state.js +0 -90
- package/dist/state.js.map +0 -1
- package/dist/stream.js +0 -558
- package/dist/stream.js.map +0 -1
package/dist/stream.js
DELETED
|
@@ -1,558 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A reactive streaming library that provides async-first data structures with built-in event streams.
|
|
3
|
-
*
|
|
4
|
-
* @template VALUE - The type of values that flow through the stream
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```typescript
|
|
8
|
-
* // Basic usage
|
|
9
|
-
* const stream = new Stream<number>();
|
|
10
|
-
* stream.listen(value => console.log(value));
|
|
11
|
-
* stream.push(1, 2, 3);
|
|
12
|
-
*
|
|
13
|
-
* // With async generator
|
|
14
|
-
* const timerStream = new Stream(async function* () {
|
|
15
|
-
* let count = 0;
|
|
16
|
-
* while (count < 5) {
|
|
17
|
-
* yield count++;
|
|
18
|
-
* await new Promise(resolve => setTimeout(resolve, 1000));
|
|
19
|
-
* }
|
|
20
|
-
* });
|
|
21
|
-
*
|
|
22
|
-
* // Async iteration
|
|
23
|
-
* for await (const value of stream) {
|
|
24
|
-
* console.log(value);
|
|
25
|
-
* if (value === 10) break;
|
|
26
|
-
* }
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
export class Stream {
|
|
30
|
-
constructor(generatorFn) {
|
|
31
|
-
this._listeners = new Set();
|
|
32
|
-
this._generatorFn = generatorFn;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Returns true if the stream has active listeners.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* const stream = new Stream<number>();
|
|
40
|
-
* console.log(stream.hasListeners); // false
|
|
41
|
-
*
|
|
42
|
-
* const cleanup = stream.listen(value => console.log(value));
|
|
43
|
-
* console.log(stream.hasListeners); // true
|
|
44
|
-
*
|
|
45
|
-
* cleanup();
|
|
46
|
-
* console.log(stream.hasListeners); // false
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
get hasListeners() {
|
|
50
|
-
return this._listeners.size > 0;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Stream that emits when a listener is added.
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* const stream = new Stream<number>();
|
|
58
|
-
* stream.listenerAdded.listen(() => console.log('Listener added'));
|
|
59
|
-
*
|
|
60
|
-
* stream.listen(value => console.log(value)); // Triggers 'Listener added'
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
get listenerAdded() {
|
|
64
|
-
if (!this._listenerAdded)
|
|
65
|
-
this._listenerAdded = new Stream();
|
|
66
|
-
return this._listenerAdded;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Stream that emits when a listener is removed.
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```typescript
|
|
73
|
-
* const stream = new Stream<number>();
|
|
74
|
-
* stream.listenerRemoved.listen(() => console.log('Listener removed'));
|
|
75
|
-
*
|
|
76
|
-
* const cleanup = stream.listen(value => console.log(value));
|
|
77
|
-
* cleanup(); // Triggers 'Listener removed'
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
get listenerRemoved() {
|
|
81
|
-
if (!this._listenerRemoved)
|
|
82
|
-
this._listenerRemoved = new Stream();
|
|
83
|
-
return this._listenerRemoved;
|
|
84
|
-
}
|
|
85
|
-
async *[Symbol.asyncIterator]() {
|
|
86
|
-
const queue = [];
|
|
87
|
-
let resolver;
|
|
88
|
-
const abort = this.listen((value) => {
|
|
89
|
-
queue.push(value);
|
|
90
|
-
resolver?.();
|
|
91
|
-
});
|
|
92
|
-
try {
|
|
93
|
-
while (true) {
|
|
94
|
-
if (queue.length)
|
|
95
|
-
yield queue.shift();
|
|
96
|
-
else
|
|
97
|
-
await new Promise((resolve) => (resolver = resolve));
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
finally {
|
|
101
|
-
abort();
|
|
102
|
-
queue.length = 0;
|
|
103
|
-
resolver = undefined;
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Pushes one or more values to all listeners.
|
|
109
|
-
*
|
|
110
|
-
* @param value - The first value to push
|
|
111
|
-
* @param values - Additional values to push
|
|
112
|
-
*
|
|
113
|
-
* @example
|
|
114
|
-
* ```typescript
|
|
115
|
-
* const stream = new Stream<number>();
|
|
116
|
-
* stream.listen(value => console.log('Received:', value));
|
|
117
|
-
*
|
|
118
|
-
* stream.push(1); // Received: 1
|
|
119
|
-
* stream.push(2, 3, 4); // Received: 2, Received: 3, Received: 4
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
push(value, ...values) {
|
|
123
|
-
values.unshift(value);
|
|
124
|
-
for (const value of values) {
|
|
125
|
-
for (const listener of this._listeners) {
|
|
126
|
-
listener(value);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Adds a listener to the stream.
|
|
132
|
-
*
|
|
133
|
-
* @param listener - Function to call when values are pushed
|
|
134
|
-
* @param signal - Optional AbortSignal for cleanup
|
|
135
|
-
* @returns Cleanup function to remove the listener
|
|
136
|
-
*
|
|
137
|
-
* @example
|
|
138
|
-
* ```typescript
|
|
139
|
-
* const stream = new Stream<string>();
|
|
140
|
-
*
|
|
141
|
-
* // Basic listener
|
|
142
|
-
* const cleanup = stream.listen(value => console.log(value));
|
|
143
|
-
*
|
|
144
|
-
* // With AbortSignal
|
|
145
|
-
* const controller = new AbortController();
|
|
146
|
-
* stream.listen(value => console.log(value), controller.signal);
|
|
147
|
-
* controller.abort(); // Removes listener
|
|
148
|
-
*
|
|
149
|
-
* // Manual cleanup
|
|
150
|
-
* cleanup();
|
|
151
|
-
* ```
|
|
152
|
-
*/
|
|
153
|
-
listen(listener, signal) {
|
|
154
|
-
if (signal?.aborted)
|
|
155
|
-
return () => { };
|
|
156
|
-
const self = this;
|
|
157
|
-
signal?.addEventListener("abort", abort);
|
|
158
|
-
self._listeners.add(listener);
|
|
159
|
-
self._listenerAdded?.push();
|
|
160
|
-
if (self._generatorFn && self._listeners.size === 1) {
|
|
161
|
-
self._generator = self._generatorFn();
|
|
162
|
-
(async () => {
|
|
163
|
-
for await (const value of self._generator) {
|
|
164
|
-
self.push(value);
|
|
165
|
-
}
|
|
166
|
-
})();
|
|
167
|
-
}
|
|
168
|
-
return abort;
|
|
169
|
-
function abort() {
|
|
170
|
-
self._listeners.delete(listener);
|
|
171
|
-
self._listenerRemoved?.push();
|
|
172
|
-
if (self._listeners.size === 0) {
|
|
173
|
-
self._generator?.return();
|
|
174
|
-
self._generator = undefined;
|
|
175
|
-
}
|
|
176
|
-
signal?.removeEventListener("abort", abort);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Promise-like interface that resolves with the next value.
|
|
181
|
-
*
|
|
182
|
-
* @param onfulfilled - Optional transformation function
|
|
183
|
-
* @returns Promise that resolves with the first value
|
|
184
|
-
*
|
|
185
|
-
* @example
|
|
186
|
-
* ```typescript
|
|
187
|
-
* const stream = new Stream<number>();
|
|
188
|
-
*
|
|
189
|
-
* setTimeout(()=>{
|
|
190
|
-
* stream.push(5);
|
|
191
|
-
* })
|
|
192
|
-
* // Wait for first value
|
|
193
|
-
* const firstValue = await stream; // Resolves promises with 5
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
|
-
then(onfulfilled) {
|
|
199
|
-
return new Promise((resolve) => {
|
|
200
|
-
const abort = this.listen((value) => {
|
|
201
|
-
resolve(value);
|
|
202
|
-
abort();
|
|
203
|
-
});
|
|
204
|
-
}).then(onfulfilled);
|
|
205
|
-
}
|
|
206
|
-
filter(predicateOrInitial, predicate) {
|
|
207
|
-
const source = this;
|
|
208
|
-
if (typeof predicateOrInitial === "function") {
|
|
209
|
-
return new Stream(async function* () {
|
|
210
|
-
for await (const value of source) {
|
|
211
|
-
if (await predicateOrInitial(value))
|
|
212
|
-
yield value;
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
return new Stream(async function* () {
|
|
218
|
-
let accumulator = predicateOrInitial;
|
|
219
|
-
for await (const value of source) {
|
|
220
|
-
const [shouldPass, newAcc] = await predicate(accumulator, value);
|
|
221
|
-
accumulator = newAcc;
|
|
222
|
-
if (shouldPass)
|
|
223
|
-
yield value;
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
map(mapperOrInitial, mapper) {
|
|
229
|
-
const source = this;
|
|
230
|
-
if (typeof mapperOrInitial === "function") {
|
|
231
|
-
return new Stream(async function* () {
|
|
232
|
-
for await (const value of source) {
|
|
233
|
-
yield await mapperOrInitial(value);
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
return new Stream(async function* () {
|
|
239
|
-
let accumulator = mapperOrInitial;
|
|
240
|
-
for await (const value of source) {
|
|
241
|
-
const [mapped, newAcc] = await mapper(accumulator, value);
|
|
242
|
-
accumulator = newAcc;
|
|
243
|
-
yield mapped;
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
group(predicateOrInitial, predicate) {
|
|
249
|
-
const source = this;
|
|
250
|
-
if (typeof predicateOrInitial === "function") {
|
|
251
|
-
return new Stream(async function* () {
|
|
252
|
-
const accumulator = [];
|
|
253
|
-
for await (const value of source) {
|
|
254
|
-
accumulator.push(value);
|
|
255
|
-
if (await predicateOrInitial([...accumulator], value)) {
|
|
256
|
-
yield [...accumulator];
|
|
257
|
-
accumulator.length = 0;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
else {
|
|
263
|
-
return new Stream(async function* () {
|
|
264
|
-
let accumulator = predicateOrInitial;
|
|
265
|
-
for await (const value of source) {
|
|
266
|
-
const [shouldEmit, newAcc] = await predicate(accumulator, value);
|
|
267
|
-
if (shouldEmit) {
|
|
268
|
-
yield accumulator;
|
|
269
|
-
accumulator = predicateOrInitial; // Reset to initial
|
|
270
|
-
}
|
|
271
|
-
else {
|
|
272
|
-
accumulator = newAcc;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* Merges this stream with other streams.
|
|
280
|
-
*
|
|
281
|
-
* @param streams - Other streams to merge with
|
|
282
|
-
* @returns New stream with values from all streams
|
|
283
|
-
*
|
|
284
|
-
* @example
|
|
285
|
-
* ```typescript
|
|
286
|
-
* const stream1 = new Stream<string>();
|
|
287
|
-
* const stream2 = new Stream<number>();
|
|
288
|
-
* const merged = stream1.merge(stream2);
|
|
289
|
-
*
|
|
290
|
-
* merged.listen(msg => console.log('Merged:', msg));
|
|
291
|
-
*
|
|
292
|
-
* stream1.push('from stream1');
|
|
293
|
-
* stream2.push(42);
|
|
294
|
-
* // Output: Merged: from stream1, Merged: 42
|
|
295
|
-
* ```
|
|
296
|
-
*/
|
|
297
|
-
merge(...streams) {
|
|
298
|
-
const source = this;
|
|
299
|
-
return new Stream(async function* () {
|
|
300
|
-
const queue = [];
|
|
301
|
-
let resolver;
|
|
302
|
-
let activeStreams = streams.length + 1;
|
|
303
|
-
async function consume(_generator) {
|
|
304
|
-
try {
|
|
305
|
-
for await (const value of _generator) {
|
|
306
|
-
queue.push(value);
|
|
307
|
-
resolver?.();
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
finally {
|
|
311
|
-
activeStreams--;
|
|
312
|
-
resolver?.();
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
const generators = [source, ...streams].map((stream) => {
|
|
316
|
-
const _generator = stream[Symbol.asyncIterator]();
|
|
317
|
-
consume(_generator);
|
|
318
|
-
return _generator;
|
|
319
|
-
});
|
|
320
|
-
try {
|
|
321
|
-
while (activeStreams > 0 || queue.length > 0) {
|
|
322
|
-
if (queue.length > 0) {
|
|
323
|
-
yield queue.shift();
|
|
324
|
-
}
|
|
325
|
-
else {
|
|
326
|
-
await new Promise((resolve) => (resolver = resolve));
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
finally {
|
|
331
|
-
for (const _generator of generators) {
|
|
332
|
-
await _generator.return();
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Flattens array values in the stream.
|
|
339
|
-
*
|
|
340
|
-
* @param depth - Depth to flatten (default: 0)
|
|
341
|
-
* @returns New stream with flattened values
|
|
342
|
-
*
|
|
343
|
-
* @example
|
|
344
|
-
* ```typescript
|
|
345
|
-
* const arrays = new Stream<number[]>();
|
|
346
|
-
* const flattened = arrays.flat();
|
|
347
|
-
*
|
|
348
|
-
* flattened.listen(n => console.log('Flat:', n));
|
|
349
|
-
* arrays.push([1, 2], [3, 4]); // Flat: 1, Flat: 2, Flat: 3, Flat: 4
|
|
350
|
-
*
|
|
351
|
-
* // Deep flattening
|
|
352
|
-
* const nested = new Stream<number[][][]>();
|
|
353
|
-
* const deepFlat = nested.flat(2);
|
|
354
|
-
* ```
|
|
355
|
-
*/
|
|
356
|
-
flat(depth = 0) {
|
|
357
|
-
const source = this;
|
|
358
|
-
return new Stream(async function* () {
|
|
359
|
-
for await (const value of source) {
|
|
360
|
-
if (Array.isArray(value)) {
|
|
361
|
-
const values = value.flat(depth);
|
|
362
|
-
for (let i = 0; i < values.length; i++) {
|
|
363
|
-
yield values[i];
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
else {
|
|
367
|
-
yield value;
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* Applies a transformer function to this stream, enabling functional composition.
|
|
374
|
-
*
|
|
375
|
-
* @param transformer - Function that takes a stream and returns a transformed stream
|
|
376
|
-
* @returns New stream with the transformation applied
|
|
377
|
-
*
|
|
378
|
-
* @example
|
|
379
|
-
* ```typescript
|
|
380
|
-
* const numbers = new Stream<number>();
|
|
381
|
-
*
|
|
382
|
-
* // Using built-in transformers
|
|
383
|
-
* const result = numbers
|
|
384
|
-
* .pipe(filter(n => n > 0))
|
|
385
|
-
* .pipe(map(n => n * 2))
|
|
386
|
-
* .pipe(group(batch => batch.length >= 3));
|
|
387
|
-
*
|
|
388
|
-
* // Custom transformer
|
|
389
|
-
* const throttle = <T>(ms: number) => (stream: Stream<T>) =>
|
|
390
|
-
* new Stream<T>(async function* () {
|
|
391
|
-
* let lastEmit = 0;
|
|
392
|
-
* for await (const value of stream) {
|
|
393
|
-
* const now = Date.now();
|
|
394
|
-
* if (now - lastEmit >= ms) {
|
|
395
|
-
* yield value;
|
|
396
|
-
* lastEmit = now;
|
|
397
|
-
* }
|
|
398
|
-
* }
|
|
399
|
-
* });
|
|
400
|
-
*
|
|
401
|
-
* numbers.pipe(throttle(1000));
|
|
402
|
-
* ```
|
|
403
|
-
*/
|
|
404
|
-
pipe(transformer) {
|
|
405
|
-
return transformer(this);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Creates a map transformer for use with pipe().
|
|
410
|
-
*
|
|
411
|
-
* @param mapper - Function to transform each value, or initial accumulator value
|
|
412
|
-
* @param mapper - Optional mapper function when using accumulator
|
|
413
|
-
* @returns Transformer function for pipe()
|
|
414
|
-
*
|
|
415
|
-
* @example
|
|
416
|
-
* ```typescript
|
|
417
|
-
* const numbers = new Stream<number>();
|
|
418
|
-
*
|
|
419
|
-
* // Simple mapping
|
|
420
|
-
* const doubled = numbers.pipe(map(n => n * 2));
|
|
421
|
-
*
|
|
422
|
-
* // Stateful mapping (running sum)
|
|
423
|
-
* const sums = numbers.pipe(map(0, (sum, n) => [sum + n, sum + n]));
|
|
424
|
-
*
|
|
425
|
-
* // Async mapping
|
|
426
|
-
* const enriched = numbers.pipe(map(async n => {
|
|
427
|
-
* const data = await fetchData(n);
|
|
428
|
-
* return { value: n, data };
|
|
429
|
-
* }));
|
|
430
|
-
* ```
|
|
431
|
-
*/
|
|
432
|
-
export const map = (mapperOrInitial, mapper) => (stream) => {
|
|
433
|
-
if (mapper) {
|
|
434
|
-
return stream.map(mapperOrInitial, mapper);
|
|
435
|
-
}
|
|
436
|
-
else {
|
|
437
|
-
return stream.map(mapperOrInitial);
|
|
438
|
-
}
|
|
439
|
-
};
|
|
440
|
-
/**
|
|
441
|
-
* Creates a filter transformer for use with pipe().
|
|
442
|
-
*
|
|
443
|
-
* @param predicate - Function to test each value, or initial accumulator value
|
|
444
|
-
* @param predicate - Optional predicate function when using accumulator
|
|
445
|
-
* @returns Transformer function for pipe()
|
|
446
|
-
*
|
|
447
|
-
* @example
|
|
448
|
-
* ```typescript
|
|
449
|
-
* const numbers = new Stream<number>();
|
|
450
|
-
*
|
|
451
|
-
* // Simple filtering
|
|
452
|
-
* const positives = numbers.pipe(filter(n => n > 0));
|
|
453
|
-
*
|
|
454
|
-
* // Type guard filtering
|
|
455
|
-
* const strings = mixed.pipe(filter((x): x is string => typeof x === 'string'));
|
|
456
|
-
*
|
|
457
|
-
* // Stateful filtering (only increasing values)
|
|
458
|
-
* const increasing = numbers.pipe(filter(0, (prev, curr) =>
|
|
459
|
-
* [curr > prev, Math.max(prev, curr)]
|
|
460
|
-
* ));
|
|
461
|
-
* ```
|
|
462
|
-
*/
|
|
463
|
-
export const filter = (predicateOrInitial, predicate) => (stream) => {
|
|
464
|
-
if (predicate) {
|
|
465
|
-
return stream.filter(predicateOrInitial, predicate);
|
|
466
|
-
}
|
|
467
|
-
else {
|
|
468
|
-
return stream.filter(predicateOrInitial);
|
|
469
|
-
}
|
|
470
|
-
};
|
|
471
|
-
/**
|
|
472
|
-
* Creates a group transformer for use with pipe().
|
|
473
|
-
*
|
|
474
|
-
* @param predicate - Function to determine when to emit a group, or initial accumulator value
|
|
475
|
-
* @param predicate - Optional predicate function when using accumulator
|
|
476
|
-
* @returns Transformer function for pipe()
|
|
477
|
-
*
|
|
478
|
-
* @example
|
|
479
|
-
* ```typescript
|
|
480
|
-
* const numbers = new Stream<number>();
|
|
481
|
-
*
|
|
482
|
-
* // Group by count
|
|
483
|
-
* const batches = numbers.pipe(group(batch => batch.length >= 5));
|
|
484
|
-
*
|
|
485
|
-
* // Group by sum
|
|
486
|
-
* const sumGroups = numbers.pipe(group(0, (sum, n) =>
|
|
487
|
-
* sum + n >= 100 ? [true, 0] : [false, sum + n]
|
|
488
|
-
* ));
|
|
489
|
-
*
|
|
490
|
-
* // Time-based grouping
|
|
491
|
-
* const timeGroups = events.pipe(group([], (window, event) => {
|
|
492
|
-
* const now = Date.now();
|
|
493
|
-
* const recent = window.filter(e => now - e.timestamp < 5000);
|
|
494
|
-
* return [recent.length >= 10, [...recent, event]];
|
|
495
|
-
* }));
|
|
496
|
-
* ```
|
|
497
|
-
*/
|
|
498
|
-
export const group = (predicateOrInitial, predicate) => (stream) => {
|
|
499
|
-
if (predicate) {
|
|
500
|
-
return stream.group(predicateOrInitial, predicate);
|
|
501
|
-
}
|
|
502
|
-
else {
|
|
503
|
-
return stream.group(predicateOrInitial);
|
|
504
|
-
}
|
|
505
|
-
};
|
|
506
|
-
/**
|
|
507
|
-
* Creates a merge transformer for use with pipe().
|
|
508
|
-
*
|
|
509
|
-
* @param streams - Additional streams to merge with the source stream
|
|
510
|
-
* @returns Transformer function for pipe()
|
|
511
|
-
*
|
|
512
|
-
* @example
|
|
513
|
-
* ```typescript
|
|
514
|
-
* const stream1 = new Stream<string>();
|
|
515
|
-
* const stream2 = new Stream<number>();
|
|
516
|
-
* const stream3 = new Stream<boolean>();
|
|
517
|
-
*
|
|
518
|
-
* // Merge multiple streams
|
|
519
|
-
* const merged = stream1.pipe(merge(stream2, stream3));
|
|
520
|
-
* // Type: Stream<string | number | boolean>
|
|
521
|
-
*
|
|
522
|
-
* merged.listen(value => {
|
|
523
|
-
* console.log('Received:', value); // Could be string, number, or boolean
|
|
524
|
-
* });
|
|
525
|
-
*
|
|
526
|
-
* stream1.push('hello');
|
|
527
|
-
* stream2.push(42);
|
|
528
|
-
* stream3.push(true);
|
|
529
|
-
* ```
|
|
530
|
-
*/
|
|
531
|
-
export const merge = (...streams) => (stream) => stream.merge(...streams);
|
|
532
|
-
/**
|
|
533
|
-
* Creates a flat transformer for use with pipe().
|
|
534
|
-
*
|
|
535
|
-
* @param depth - Optional depth to flatten (default: 0 for single level)
|
|
536
|
-
* @returns Transformer function for pipe()
|
|
537
|
-
*
|
|
538
|
-
* @example
|
|
539
|
-
* ```typescript
|
|
540
|
-
* const arrays = new Stream<number[]>();
|
|
541
|
-
*
|
|
542
|
-
* // Flatten single level
|
|
543
|
-
* const flattened = arrays.pipe(flat());
|
|
544
|
-
* arrays.push([1, 2], [3, 4]); // Emits: 1, 2, 3, 4
|
|
545
|
-
*
|
|
546
|
-
* // Flatten multiple levels
|
|
547
|
-
* const nested = new Stream<number[][][]>();
|
|
548
|
-
* const deepFlat = nested.pipe(flat(2));
|
|
549
|
-
* nested.push([[[1, 2]], [[3, 4]]]); // Emits: 1, 2, 3, 4
|
|
550
|
-
*
|
|
551
|
-
* // Mixed content (non-arrays pass through)
|
|
552
|
-
* const mixed = new Stream<number | number[]>();
|
|
553
|
-
* const result = mixed.pipe(flat());
|
|
554
|
-
* mixed.push(1, [2, 3], 4); // Emits: 1, 2, 3, 4
|
|
555
|
-
* ```
|
|
556
|
-
*/
|
|
557
|
-
export const flat = ((depth) => (stream) => depth !== undefined ? stream.flat(depth) : stream.flat());
|
|
558
|
-
//# sourceMappingURL=stream.js.map
|
package/dist/stream.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,OAAO,MAAM;IAwCjB,YAAY,WAA+C;QAvCjD,eAAU,GAAG,IAAI,GAAG,EAA0B,CAAC;QAwCvD,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;IAClC,CAAC;IACD;;;;;;;;;;;;;;OAcG;IACH,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,GAAG,CAAC,CAAC;IAClC,CAAC;IACD;;;;;;;;;;OAUG;IACH,IAAI,aAAa;QACf,IAAI,CAAC,IAAI,CAAC,cAAc;YAAE,IAAI,CAAC,cAAc,GAAG,IAAI,MAAM,EAAQ,CAAC;QACnE,OAAO,IAAI,CAAC,cAAc,CAAC;IAC7B,CAAC;IACD;;;;;;;;;;;OAWG;IACH,IAAI,eAAe;QACjB,IAAI,CAAC,IAAI,CAAC,gBAAgB;YAAE,IAAI,CAAC,gBAAgB,GAAG,IAAI,MAAM,EAAQ,CAAC;QACvE,OAAO,IAAI,CAAC,gBAAgB,CAAC;IAC/B,CAAC;IACD,KAAK,CAAC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