@soffinal/stream 0.1.4 → 0.2.1
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/CHANGELOG.md +22 -0
- package/README.md +439 -298
- package/dist/index.d.ts +2 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +12 -8
- package/dist/reactive/index.d.ts +5 -0
- package/dist/reactive/index.d.ts.map +1 -0
- package/dist/{list.d.ts → reactive/list.d.ts} +19 -1
- package/dist/reactive/list.d.ts.map +1 -0
- package/dist/{map.d.ts → reactive/map.d.ts} +11 -1
- package/dist/reactive/map.d.ts.map +1 -0
- package/dist/{set.d.ts → reactive/set.d.ts} +11 -1
- package/dist/reactive/set.d.ts.map +1 -0
- package/dist/{state.d.ts → reactive/state.d.ts} +18 -18
- package/dist/reactive/state.d.ts.map +1 -0
- package/dist/stream.d.ts +94 -289
- package/dist/stream.d.ts.map +1 -1
- package/dist/transformers/filter.d.ts +35 -0
- package/dist/transformers/filter.d.ts.map +1 -0
- package/dist/transformers/flat.d.ts +31 -0
- package/dist/transformers/flat.d.ts.map +1 -0
- package/dist/transformers/index.d.ts +5 -0
- package/dist/transformers/index.d.ts.map +1 -0
- package/dist/transformers/map.d.ts +36 -0
- package/dist/transformers/map.d.ts.map +1 -0
- package/dist/transformers/merge.d.ts +35 -0
- package/dist/transformers/merge.d.ts.map +1 -0
- package/package.json +5 -8
- package/src/transformers/filter.md +202 -0
- package/src/transformers/flat.md +56 -0
- package/src/transformers/map.md +216 -0
- package/src/transformers/merge.md +79 -0
- package/dist/benchmark.d.ts +0 -16
- package/dist/benchmark.d.ts.map +0 -1
- package/dist/list.d.ts.map +0 -1
- package/dist/map.d.ts.map +0 -1
- package/dist/set.d.ts.map +0 -1
- package/dist/state.d.ts.map +0 -1
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { Stream } from "
|
|
1
|
+
import { FunctionGenerator, Stream } from "../stream.ts";
|
|
2
2
|
/**
|
|
3
3
|
* A reactive state container that extends Stream to provide stateful value management.
|
|
4
4
|
*
|
|
5
5
|
* @template VALUE - The type of the state value
|
|
6
6
|
*
|
|
7
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
8
|
+
*
|
|
7
9
|
* @example
|
|
8
10
|
* ```typescript
|
|
9
11
|
* // Basic state
|
|
@@ -11,35 +13,29 @@ import { Stream } from "./stream.ts";
|
|
|
11
13
|
* counter.listen(value => console.log('Counter:', value));
|
|
12
14
|
* counter.value = 5; // Counter: 5
|
|
13
15
|
*
|
|
14
|
-
* //
|
|
15
|
-
*
|
|
16
|
-
* const
|
|
16
|
+
* // State from stream
|
|
17
|
+
* const source = new Stream<number>();
|
|
18
|
+
* const state = new State(0, source);
|
|
19
|
+
* state.listen(value => console.log('State:', value));
|
|
20
|
+
* source.push(1, 2, 3); // State: 1, State: 2, State: 3
|
|
17
21
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
22
|
+
* // State from transformed stream
|
|
23
|
+
* const filtered = source.pipe(filter({}, (_, v) => [v > 0, {}]));
|
|
24
|
+
* const derivedState = new State(-1, filtered);
|
|
20
25
|
* ```
|
|
21
26
|
*/
|
|
22
27
|
export declare class State<VALUE> extends Stream<VALUE> {
|
|
23
28
|
protected _value: VALUE;
|
|
24
|
-
/**
|
|
25
|
-
* Creates a new State with an initial value.
|
|
26
|
-
*
|
|
27
|
-
* @param initialValue - The initial state value
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```typescript
|
|
31
|
-
* const count = new State(0);
|
|
32
|
-
* const theme = new State<'light' | 'dark'>('light');
|
|
33
|
-
* const user = new State<User | null>(null);
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
29
|
constructor(initialValue: VALUE);
|
|
30
|
+
constructor(initialValue: VALUE, stream: FunctionGenerator<VALUE> | Stream<VALUE>);
|
|
37
31
|
/**
|
|
38
32
|
* Updates the state with one or more values sequentially.
|
|
39
33
|
* Each value triggers listeners and updates the current state.
|
|
40
34
|
*
|
|
41
35
|
* @param values - Values to set as state
|
|
42
36
|
*
|
|
37
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
38
|
+
*
|
|
43
39
|
* @example
|
|
44
40
|
* ```typescript
|
|
45
41
|
* const state = new State(0);
|
|
@@ -53,6 +49,8 @@ export declare class State<VALUE> extends Stream<VALUE> {
|
|
|
53
49
|
/**
|
|
54
50
|
* Gets the current state value.
|
|
55
51
|
*
|
|
52
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
53
|
+
*
|
|
56
54
|
* @example
|
|
57
55
|
* ```typescript
|
|
58
56
|
* const state = new State('hello');
|
|
@@ -65,6 +63,8 @@ export declare class State<VALUE> extends Stream<VALUE> {
|
|
|
65
63
|
*
|
|
66
64
|
* @param value - The new state value
|
|
67
65
|
*
|
|
66
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
67
|
+
*
|
|
68
68
|
* @example
|
|
69
69
|
* ```typescript
|
|
70
70
|
* const state = new State(0);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../src/reactive/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,KAAK,CAAC,KAAK,CAAE,SAAQ,MAAM,CAAC,KAAK,CAAC;IAC7C,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC;gBACZ,YAAY,EAAE,KAAK;gBACnB,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC;IAmBjF;;;;;;;;;;;;;;;;OAgBG;IACM,IAAI,CAAC,GAAG,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI;IAKvC;;;;;;;;;;OAUG;IACH,IAAI,KAAK,IAAI,KAAK,CAEjB;IACD;;;;;;;;;;;;;;;OAeG;IACH,IAAI,KAAK,CAAC,KAAK,EAAE,KAAK,EAGrB;CACF"}
|
package/dist/stream.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
export type ValueOf<STREAM> = STREAM extends Stream<infer VALUE> ? VALUE : never;
|
|
2
|
-
export type
|
|
3
|
-
"done": Arr;
|
|
4
|
-
"recur": Arr extends ReadonlyArray<infer InnerArr> ? FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][Depth]> : Arr;
|
|
5
|
-
}[Depth extends -1 ? "done" : "recur"];
|
|
2
|
+
export type FunctionGenerator<VALUE> = () => AsyncGenerator<VALUE, void>;
|
|
6
3
|
/**
|
|
7
4
|
* A reactive streaming library that provides async-first data structures with built-in event streams.
|
|
8
5
|
*
|
|
@@ -30,10 +27,74 @@ export type FlatArray<Arr, Depth extends number> = {
|
|
|
30
27
|
* if (value === 10) break;
|
|
31
28
|
* }
|
|
32
29
|
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // 📦 COPY-PASTE TRANSFORMERS LIBRARY - Essential transformers for immediate use
|
|
33
|
+
*
|
|
34
|
+
* // FILTERING TRANSFORMERS
|
|
35
|
+
* const simpleFilter = <T>(predicate: (value: T) => boolean | Promise<boolean>) =>
|
|
36
|
+
* filter<T, {}>({}, async (_, value) => {
|
|
37
|
+
* const shouldPass = await predicate(value);
|
|
38
|
+
* return [shouldPass, {}];
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* const take = <T>(n: number) =>
|
|
42
|
+
* filter<T, { count: number }>({ count: 0 }, (state, value) => {
|
|
43
|
+
* if (state.count >= n) return;
|
|
44
|
+
* return [true, { count: state.count + 1 }];
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* const distinct = <T>() =>
|
|
48
|
+
* filter<T, { seen: Set<T> }>({ seen: new Set() }, (state, value) => {
|
|
49
|
+
* if (state.seen.has(value)) return [false, state];
|
|
50
|
+
* state.seen.add(value);
|
|
51
|
+
* return [true, state];
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* const tap = <T>(fn: (value: T) => void | Promise<void>) =>
|
|
55
|
+
* filter<T, {}>({}, async (_, value) => {
|
|
56
|
+
* await fn(value);
|
|
57
|
+
* return [true, {}];
|
|
58
|
+
* });
|
|
59
|
+
*
|
|
60
|
+
* // MAPPING TRANSFORMERS
|
|
61
|
+
* const simpleMap = <T, U>(fn: (value: T) => U | Promise<U>) =>
|
|
62
|
+
* map<T, {}, U>({}, async (_, value) => {
|
|
63
|
+
* const result = await fn(value);
|
|
64
|
+
* return [result, {}];
|
|
65
|
+
* });
|
|
66
|
+
*
|
|
67
|
+
* const withIndex = <T>() =>
|
|
68
|
+
* map<T, { index: number }, { value: T; index: number }>(
|
|
69
|
+
* { index: 0 },
|
|
70
|
+
* (state, value) => [
|
|
71
|
+
* { value, index: state.index },
|
|
72
|
+
* { index: state.index + 1 }
|
|
73
|
+
* ]
|
|
74
|
+
* );
|
|
75
|
+
*
|
|
76
|
+
* const delay = <T>(ms: number) =>
|
|
77
|
+
* map<T, {}, T>({}, async (_, value) => {
|
|
78
|
+
* await new Promise(resolve => setTimeout(resolve, ms));
|
|
79
|
+
* return [value, {}];
|
|
80
|
+
* });
|
|
81
|
+
*
|
|
82
|
+
* const scan = <T, U>(fn: (acc: U, value: T) => U, initial: U) =>
|
|
83
|
+
* map<T, { acc: U }, U>({ acc: initial }, (state, value) => {
|
|
84
|
+
* const newAcc = fn(state.acc, value);
|
|
85
|
+
* return [newAcc, { acc: newAcc }];
|
|
86
|
+
* });
|
|
87
|
+
*
|
|
88
|
+
* // STATE CONVERTER
|
|
89
|
+
* const toState = <T>(initialValue: T) => (stream: Stream<T>) => {
|
|
90
|
+
* return new State(initialValue, stream);
|
|
91
|
+
* };
|
|
92
|
+
*
|
|
93
|
+
* // Usage: stream.pipe(simpleFilter(x => x > 0)).pipe(take(5)).pipe(toState(0));
|
|
33
94
|
*/
|
|
34
95
|
export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
35
96
|
protected _listeners: Set<(value: VALUE) => void>;
|
|
36
|
-
protected _generatorFn:
|
|
97
|
+
protected _generatorFn: FunctionGenerator<VALUE> | undefined;
|
|
37
98
|
protected _generator: AsyncGenerator<VALUE, void> | undefined;
|
|
38
99
|
protected _listenerAdded: Stream<void> | undefined;
|
|
39
100
|
protected _listenerRemoved: Stream<void> | undefined;
|
|
@@ -42,6 +103,8 @@ export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
|
42
103
|
*
|
|
43
104
|
* @param generatorFn - Optional async generator function to produce values you can use it for creating stream with custom transformation
|
|
44
105
|
*
|
|
106
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
107
|
+
*
|
|
45
108
|
* @example
|
|
46
109
|
* ```typescript
|
|
47
110
|
* // Empty stream
|
|
@@ -70,10 +133,12 @@ export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
|
70
133
|
* ```
|
|
71
134
|
*/
|
|
72
135
|
constructor();
|
|
73
|
-
constructor(
|
|
136
|
+
constructor(stream: FunctionGenerator<VALUE> | Stream<VALUE>);
|
|
74
137
|
/**
|
|
75
138
|
* Returns true if the stream has active listeners.
|
|
76
139
|
*
|
|
140
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
141
|
+
*
|
|
77
142
|
* @example
|
|
78
143
|
* ```typescript
|
|
79
144
|
* const stream = new Stream<number>();
|
|
@@ -90,6 +155,8 @@ export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
|
90
155
|
/**
|
|
91
156
|
* Stream that emits when a listener is added.
|
|
92
157
|
*
|
|
158
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
159
|
+
*
|
|
93
160
|
* @example
|
|
94
161
|
* ```typescript
|
|
95
162
|
* const stream = new Stream<number>();
|
|
@@ -102,6 +169,8 @@ export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
|
102
169
|
/**
|
|
103
170
|
* Stream that emits when a listener is removed.
|
|
104
171
|
*
|
|
172
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
173
|
+
*
|
|
105
174
|
* @example
|
|
106
175
|
* ```typescript
|
|
107
176
|
* const stream = new Stream<number>();
|
|
@@ -116,6 +185,8 @@ export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
|
116
185
|
/**
|
|
117
186
|
* Pushes one or more values to all listeners.
|
|
118
187
|
*
|
|
188
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
189
|
+
*
|
|
119
190
|
* @param value - The first value to push
|
|
120
191
|
* @param values - Additional values to push
|
|
121
192
|
*
|
|
@@ -136,6 +207,8 @@ export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
|
136
207
|
* @param signal - Optional AbortSignal for cleanup
|
|
137
208
|
* @returns Cleanup function to remove the listener
|
|
138
209
|
*
|
|
210
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
211
|
+
*
|
|
139
212
|
* @example
|
|
140
213
|
* ```typescript
|
|
141
214
|
* const stream = new Stream<string>();
|
|
@@ -152,13 +225,15 @@ export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
|
152
225
|
* cleanup();
|
|
153
226
|
* ```
|
|
154
227
|
*/
|
|
155
|
-
listen(listener: (value: VALUE) => void, signal?: AbortSignal): () => void;
|
|
228
|
+
listen(listener: (value: VALUE) => void, signal?: AbortSignal | Stream<any>): () => void;
|
|
156
229
|
/**
|
|
157
230
|
* Promise-like interface that resolves with the next value.
|
|
158
231
|
*
|
|
159
232
|
* @param onfulfilled - Optional transformation function
|
|
160
233
|
* @returns Promise that resolves with the first value
|
|
161
234
|
*
|
|
235
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
236
|
+
*
|
|
162
237
|
* @example
|
|
163
238
|
* ```typescript
|
|
164
239
|
* const stream = new Stream<number>();
|
|
@@ -173,126 +248,23 @@ export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
|
173
248
|
* ```
|
|
174
249
|
*/
|
|
175
250
|
then(onfulfilled?: ((value: VALUE) => VALUE | PromiseLike<VALUE>) | null): Promise<VALUE>;
|
|
176
|
-
/**
|
|
177
|
-
* Filters values based on a predicate function.
|
|
178
|
-
*
|
|
179
|
-
* @param predicate - Function to test each value
|
|
180
|
-
* @returns New stream with filtered values
|
|
181
|
-
*
|
|
182
|
-
* @example
|
|
183
|
-
* ```typescript
|
|
184
|
-
* const numbers = new Stream<number>();
|
|
185
|
-
* const evens = numbers.filter(n => n % 2 === 0);
|
|
186
|
-
*
|
|
187
|
-
* evens.listen(n => console.log('Even:', n));
|
|
188
|
-
* numbers.push(1, 2, 3, 4); // Even: 2, Even: 4
|
|
189
|
-
*
|
|
190
|
-
* // With accumulator
|
|
191
|
-
* const increasing = numbers.filter(0, (prev, curr) =>
|
|
192
|
-
* [curr > prev, Math.max(prev, curr)]
|
|
193
|
-
* );
|
|
194
|
-
* ```
|
|
195
|
-
*/
|
|
196
|
-
filter<FILTERED extends VALUE>(predicate: (value: VALUE) => value is FILTERED): Stream<FILTERED>;
|
|
197
|
-
filter<FILTERED extends VALUE>(predicate: (value: VALUE) => boolean | Promise<boolean>): Stream<FILTERED>;
|
|
198
|
-
filter<FILTERED extends VALUE, ACC>(initialValue: ACC, predicate: (accumulator: ACC, value: VALUE) => [boolean, ACC] | Promise<[boolean, ACC]>): Stream<FILTERED>;
|
|
199
|
-
/**
|
|
200
|
-
* Transforms values using a mapper function.
|
|
201
|
-
*
|
|
202
|
-
* @param mapper - Function to transform each value
|
|
203
|
-
* @returns New stream with transformed values
|
|
204
|
-
*
|
|
205
|
-
* @example
|
|
206
|
-
* ```typescript
|
|
207
|
-
* const numbers = new Stream<number>();
|
|
208
|
-
* const doubled = numbers.map(n => n * 2);
|
|
209
|
-
*
|
|
210
|
-
* doubled.listen(n => console.log('Doubled:', n));
|
|
211
|
-
* numbers.push(1, 2, 3); // Doubled: 2, Doubled: 4, Doubled: 6
|
|
212
|
-
*
|
|
213
|
-
* // With accumulator (running sum)
|
|
214
|
-
* const sums = numbers.map(0, (sum, n) => [sum + n, sum + n]);
|
|
215
|
-
* ```
|
|
216
|
-
*/
|
|
217
|
-
map<MAPPED>(mapper: (value: VALUE) => MAPPED | Promise<MAPPED>): Stream<MAPPED>;
|
|
218
|
-
map<MAPPED, ACC>(initialValue: ACC, mapper: (accumulator: ACC, value: VALUE) => [MAPPED, ACC] | Promise<[MAPPED, ACC]>): Stream<MAPPED>;
|
|
219
|
-
/**
|
|
220
|
-
* Groups values into batches based on a predicate.
|
|
221
|
-
*
|
|
222
|
-
* @param predicate - Function to determine when to emit a group
|
|
223
|
-
* @returns New stream with grouped values
|
|
224
|
-
*
|
|
225
|
-
* @example
|
|
226
|
-
* ```typescript
|
|
227
|
-
* const numbers = new Stream<number>();
|
|
228
|
-
* const batches = numbers.group(batch => batch.length === 3);
|
|
229
|
-
*
|
|
230
|
-
* batches.listen(batch => console.log('Batch:', batch));
|
|
231
|
-
* numbers.push(1, 2, 3, 4, 5, 6); // Batch: [1,2,3], Batch: [4,5,6]
|
|
232
|
-
*
|
|
233
|
-
* // With accumulator (sum-based grouping)
|
|
234
|
-
* const sumGroups = numbers.group(0, (sum, n) =>
|
|
235
|
-
* sum + n >= 10 ? [true, 0] : [false, sum + n]
|
|
236
|
-
* );
|
|
237
|
-
* ```
|
|
238
|
-
*/
|
|
239
|
-
group(predicate: (accumulator: VALUE[], value: VALUE) => boolean | Promise<boolean>): Stream<VALUE[]>;
|
|
240
|
-
group<ACC>(initialValue: ACC, predicate: (accumulator: ACC, value: VALUE) => [boolean, ACC] | Promise<[boolean, ACC]>): Stream<ACC>;
|
|
241
|
-
/**
|
|
242
|
-
* Merges this stream with other streams.
|
|
243
|
-
*
|
|
244
|
-
* @param streams - Other streams to merge with
|
|
245
|
-
* @returns New stream with values from all streams
|
|
246
|
-
*
|
|
247
|
-
* @example
|
|
248
|
-
* ```typescript
|
|
249
|
-
* const stream1 = new Stream<string>();
|
|
250
|
-
* const stream2 = new Stream<number>();
|
|
251
|
-
* const merged = stream1.merge(stream2);
|
|
252
|
-
*
|
|
253
|
-
* merged.listen(msg => console.log('Merged:', msg));
|
|
254
|
-
*
|
|
255
|
-
* stream1.push('from stream1');
|
|
256
|
-
* stream2.push(42);
|
|
257
|
-
* // Output: Merged: from stream1, Merged: 42
|
|
258
|
-
* ```
|
|
259
|
-
*/
|
|
260
|
-
merge<STREAMS extends [Stream<any>, ...Stream<any>[]]>(...streams: STREAMS): Stream<VALUE | ValueOf<STREAMS[number]>>;
|
|
261
|
-
/**
|
|
262
|
-
* Flattens array values in the stream.
|
|
263
|
-
*
|
|
264
|
-
* @param depth - Depth to flatten (default: 0)
|
|
265
|
-
* @returns New stream with flattened values
|
|
266
|
-
*
|
|
267
|
-
* @example
|
|
268
|
-
* ```typescript
|
|
269
|
-
* const arrays = new Stream<number[]>();
|
|
270
|
-
* const flattened = arrays.flat();
|
|
271
|
-
*
|
|
272
|
-
* flattened.listen(n => console.log('Flat:', n));
|
|
273
|
-
* arrays.push([1, 2], [3, 4]); // Flat: 1, Flat: 2, Flat: 3, Flat: 4
|
|
274
|
-
*
|
|
275
|
-
* // Deep flattening
|
|
276
|
-
* const nested = new Stream<number[][][]>();
|
|
277
|
-
* const deepFlat = nested.flat(2);
|
|
278
|
-
* ```
|
|
279
|
-
*/
|
|
280
|
-
flat<DEPTH extends number = 0>(depth?: DEPTH): Stream<FlatArray<VALUE, DEPTH>>;
|
|
281
251
|
/**
|
|
282
252
|
* Applies a transformer function to this stream, enabling functional composition.
|
|
283
253
|
*
|
|
284
|
-
* @param transformer - Function that takes a stream and returns
|
|
285
|
-
* @returns
|
|
254
|
+
* @param transformer - Function that takes a stream and returns any output type
|
|
255
|
+
* @returns The result of the transformer function
|
|
256
|
+
*
|
|
257
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
286
258
|
*
|
|
287
259
|
* @example
|
|
288
260
|
* ```typescript
|
|
289
261
|
* const numbers = new Stream<number>();
|
|
290
262
|
*
|
|
291
|
-
* //
|
|
263
|
+
* // Chain transformers
|
|
292
264
|
* const result = numbers
|
|
293
|
-
* .pipe(filter(n => n > 0))
|
|
294
|
-
* .pipe(map(n => n * 2))
|
|
295
|
-
* .pipe(
|
|
265
|
+
* .pipe(filter({}, (_, n) => [n > 0, {}]))
|
|
266
|
+
* .pipe(map({}, (_, n) => [n * 2, {}]))
|
|
267
|
+
* .pipe(toState(0));
|
|
296
268
|
*
|
|
297
269
|
* // Custom transformer
|
|
298
270
|
* const throttle = <T>(ms: number) => (stream: Stream<T>) =>
|
|
@@ -307,178 +279,11 @@ export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
|
307
279
|
* }
|
|
308
280
|
* });
|
|
309
281
|
*
|
|
310
|
-
*
|
|
282
|
+
* // Transform to any type
|
|
283
|
+
* const stringResult = numbers.pipe(throttle(1000));
|
|
284
|
+
* const stateResult = numbers.pipe(toState(0));
|
|
311
285
|
* ```
|
|
312
286
|
*/
|
|
313
|
-
pipe<OUTPUT
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Transformer function interface for mapping stream values.
|
|
317
|
-
* Supports both simple mapping and stateful mapping with accumulators.
|
|
318
|
-
*/
|
|
319
|
-
export interface MapFunction {
|
|
320
|
-
<VALUE, MAPPED>(mapper: (value: VALUE) => MAPPED | Promise<MAPPED>): (stream: Stream<VALUE>) => Stream<MAPPED>;
|
|
321
|
-
<VALUE, MAPPED, ACC>(initialValue: ACC, mapper: (accumulator: ACC, value: VALUE) => [MAPPED, ACC] | Promise<[MAPPED, ACC]>): (stream: Stream<VALUE>) => Stream<MAPPED>;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Creates a map transformer for use with pipe().
|
|
325
|
-
*
|
|
326
|
-
* @param mapper - Function to transform each value, or initial accumulator value
|
|
327
|
-
* @param mapper - Optional mapper function when using accumulator
|
|
328
|
-
* @returns Transformer function for pipe()
|
|
329
|
-
*
|
|
330
|
-
* @example
|
|
331
|
-
* ```typescript
|
|
332
|
-
* const numbers = new Stream<number>();
|
|
333
|
-
*
|
|
334
|
-
* // Simple mapping
|
|
335
|
-
* const doubled = numbers.pipe(map(n => n * 2));
|
|
336
|
-
*
|
|
337
|
-
* // Stateful mapping (running sum)
|
|
338
|
-
* const sums = numbers.pipe(map(0, (sum, n) => [sum + n, sum + n]));
|
|
339
|
-
*
|
|
340
|
-
* // Async mapping
|
|
341
|
-
* const enriched = numbers.pipe(map(async n => {
|
|
342
|
-
* const data = await fetchData(n);
|
|
343
|
-
* return { value: n, data };
|
|
344
|
-
* }));
|
|
345
|
-
* ```
|
|
346
|
-
*/
|
|
347
|
-
export declare const map: MapFunction;
|
|
348
|
-
/**
|
|
349
|
-
* Transformer function interface for filtering stream values.
|
|
350
|
-
* Supports type guards, boolean predicates, and stateful filtering.
|
|
351
|
-
*/
|
|
352
|
-
export interface FilterFunction {
|
|
353
|
-
<VALUE, FILTERED extends VALUE>(predicate: (value: VALUE) => value is FILTERED): (stream: Stream<VALUE>) => Stream<FILTERED>;
|
|
354
|
-
<VALUE>(predicate: (value: VALUE) => boolean | Promise<boolean>): (stream: Stream<VALUE>) => Stream<VALUE>;
|
|
355
|
-
<VALUE, ACC>(initialValue: ACC, predicate: (accumulator: ACC, value: VALUE) => [boolean, ACC] | Promise<[boolean, ACC]>): (stream: Stream<VALUE>) => Stream<VALUE>;
|
|
287
|
+
pipe<OUTPUT extends Stream<any>>(transformer: (stream: this) => OUTPUT): OUTPUT;
|
|
356
288
|
}
|
|
357
|
-
/**
|
|
358
|
-
* Creates a filter transformer for use with pipe().
|
|
359
|
-
*
|
|
360
|
-
* @param predicate - Function to test each value, or initial accumulator value
|
|
361
|
-
* @param predicate - Optional predicate function when using accumulator
|
|
362
|
-
* @returns Transformer function for pipe()
|
|
363
|
-
*
|
|
364
|
-
* @example
|
|
365
|
-
* ```typescript
|
|
366
|
-
* const numbers = new Stream<number>();
|
|
367
|
-
*
|
|
368
|
-
* // Simple filtering
|
|
369
|
-
* const positives = numbers.pipe(filter(n => n > 0));
|
|
370
|
-
*
|
|
371
|
-
* // Type guard filtering
|
|
372
|
-
* const strings = mixed.pipe(filter((x): x is string => typeof x === 'string'));
|
|
373
|
-
*
|
|
374
|
-
* // Stateful filtering (only increasing values)
|
|
375
|
-
* const increasing = numbers.pipe(filter(0, (prev, curr) =>
|
|
376
|
-
* [curr > prev, Math.max(prev, curr)]
|
|
377
|
-
* ));
|
|
378
|
-
* ```
|
|
379
|
-
*/
|
|
380
|
-
export declare const filter: FilterFunction;
|
|
381
|
-
/**
|
|
382
|
-
* Transformer function interface for grouping stream values into batches.
|
|
383
|
-
* Supports both simple batching and stateful grouping with accumulators.
|
|
384
|
-
*/
|
|
385
|
-
export interface GroupFunction {
|
|
386
|
-
<VALUE>(predicate: (accumulator: VALUE[], value: VALUE) => boolean | Promise<boolean>): (stream: Stream<VALUE>) => Stream<VALUE[]>;
|
|
387
|
-
<VALUE, ACC>(initialValue: ACC, predicate: (accumulator: ACC, value: VALUE) => [boolean, ACC] | Promise<[boolean, ACC]>): (stream: Stream<VALUE>) => Stream<ACC>;
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* Creates a group transformer for use with pipe().
|
|
391
|
-
*
|
|
392
|
-
* @param predicate - Function to determine when to emit a group, or initial accumulator value
|
|
393
|
-
* @param predicate - Optional predicate function when using accumulator
|
|
394
|
-
* @returns Transformer function for pipe()
|
|
395
|
-
*
|
|
396
|
-
* @example
|
|
397
|
-
* ```typescript
|
|
398
|
-
* const numbers = new Stream<number>();
|
|
399
|
-
*
|
|
400
|
-
* // Group by count
|
|
401
|
-
* const batches = numbers.pipe(group(batch => batch.length >= 5));
|
|
402
|
-
*
|
|
403
|
-
* // Group by sum
|
|
404
|
-
* const sumGroups = numbers.pipe(group(0, (sum, n) =>
|
|
405
|
-
* sum + n >= 100 ? [true, 0] : [false, sum + n]
|
|
406
|
-
* ));
|
|
407
|
-
*
|
|
408
|
-
* // Time-based grouping
|
|
409
|
-
* const timeGroups = events.pipe(group([], (window, event) => {
|
|
410
|
-
* const now = Date.now();
|
|
411
|
-
* const recent = window.filter(e => now - e.timestamp < 5000);
|
|
412
|
-
* return [recent.length >= 10, [...recent, event]];
|
|
413
|
-
* }));
|
|
414
|
-
* ```
|
|
415
|
-
*/
|
|
416
|
-
export declare const group: GroupFunction;
|
|
417
|
-
/**
|
|
418
|
-
* Transformer function interface for merging multiple streams.
|
|
419
|
-
* Combines values from all streams into a single output stream.
|
|
420
|
-
*/
|
|
421
|
-
export interface MergeFunction {
|
|
422
|
-
<STREAMS extends [Stream<any>, ...Stream<any>[]]>(...streams: STREAMS): <VALUE>(stream: Stream<VALUE>) => Stream<VALUE | ValueOf<STREAMS[number]>>;
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* Creates a merge transformer for use with pipe().
|
|
426
|
-
*
|
|
427
|
-
* @param streams - Additional streams to merge with the source stream
|
|
428
|
-
* @returns Transformer function for pipe()
|
|
429
|
-
*
|
|
430
|
-
* @example
|
|
431
|
-
* ```typescript
|
|
432
|
-
* const stream1 = new Stream<string>();
|
|
433
|
-
* const stream2 = new Stream<number>();
|
|
434
|
-
* const stream3 = new Stream<boolean>();
|
|
435
|
-
*
|
|
436
|
-
* // Merge multiple streams
|
|
437
|
-
* const merged = stream1.pipe(merge(stream2, stream3));
|
|
438
|
-
* // Type: Stream<string | number | boolean>
|
|
439
|
-
*
|
|
440
|
-
* merged.listen(value => {
|
|
441
|
-
* console.log('Received:', value); // Could be string, number, or boolean
|
|
442
|
-
* });
|
|
443
|
-
*
|
|
444
|
-
* stream1.push('hello');
|
|
445
|
-
* stream2.push(42);
|
|
446
|
-
* stream3.push(true);
|
|
447
|
-
* ```
|
|
448
|
-
*/
|
|
449
|
-
export declare const merge: MergeFunction;
|
|
450
|
-
/**
|
|
451
|
-
* Transformer function interface for flattening array values in streams.
|
|
452
|
-
* Supports configurable depth for nested array flattening.
|
|
453
|
-
*/
|
|
454
|
-
export interface FlatFunction {
|
|
455
|
-
(): <VALUE>(stream: Stream<VALUE>) => Stream<FlatArray<VALUE, 0>>;
|
|
456
|
-
<DEPTH extends number>(depth: DEPTH): <VALUE>(stream: Stream<VALUE>) => Stream<FlatArray<VALUE, DEPTH>>;
|
|
457
|
-
}
|
|
458
|
-
/**
|
|
459
|
-
* Creates a flat transformer for use with pipe().
|
|
460
|
-
*
|
|
461
|
-
* @param depth - Optional depth to flatten (default: 0 for single level)
|
|
462
|
-
* @returns Transformer function for pipe()
|
|
463
|
-
*
|
|
464
|
-
* @example
|
|
465
|
-
* ```typescript
|
|
466
|
-
* const arrays = new Stream<number[]>();
|
|
467
|
-
*
|
|
468
|
-
* // Flatten single level
|
|
469
|
-
* const flattened = arrays.pipe(flat());
|
|
470
|
-
* arrays.push([1, 2], [3, 4]); // Emits: 1, 2, 3, 4
|
|
471
|
-
*
|
|
472
|
-
* // Flatten multiple levels
|
|
473
|
-
* const nested = new Stream<number[][][]>();
|
|
474
|
-
* const deepFlat = nested.pipe(flat(2));
|
|
475
|
-
* nested.push([[[1, 2]], [[3, 4]]]); // Emits: 1, 2, 3, 4
|
|
476
|
-
*
|
|
477
|
-
* // Mixed content (non-arrays pass through)
|
|
478
|
-
* const mixed = new Stream<number | number[]>();
|
|
479
|
-
* const result = mixed.pipe(flat());
|
|
480
|
-
* mixed.push(1, [2, 3], 4); // Emits: 1, 2, 3, 4
|
|
481
|
-
* ```
|
|
482
|
-
*/
|
|
483
|
-
export declare const flat: FlatFunction;
|
|
484
289
|
//# sourceMappingURL=stream.d.ts.map
|
package/dist/stream.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,CAAC,MAAM,IAAI,MAAM,SAAS,MAAM,CAAC,MAAM,KAAK,CAAC,GAAG,KAAK,GAAG,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,CAAC,MAAM,IAAI,MAAM,SAAS,MAAM,CAAC,MAAM,KAAK,CAAC,GAAG,KAAK,GAAG,KAAK,CAAC;AACjF,MAAM,MAAM,iBAAiB,CAAC,KAAK,IAAI,MAAM,cAAc,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AACzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2FG;AACH,qBAAa,MAAM,CAAC,KAAK,GAAG,OAAO,CAAE,YAAW,aAAa,CAAC,KAAK,CAAC;IAClE,SAAS,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC,CAAqC;IACtF,SAAS,CAAC,YAAY,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;IAC7D,SAAS,CAAC,UAAU,EAAE,cAAc,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,SAAS,CAAC;IAC9D,SAAS,CAAC,cAAc,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;IACnD,SAAS,CAAC,gBAAgB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;IAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;;gBAES,MAAM,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC;IAK5D;;;;;;;;;;;;;;;;OAgBG;IACH,IAAI,YAAY,IAAI,OAAO,CAE1B;IAED;;;;;;;;;;;;OAYG;IACH,IAAI,aAAa,IAAI,MAAM,CAAC,IAAI,CAAC,CAGhC;IAED;;;;;;;;;;;;;OAaG;IACH,IAAI,eAAe,IAAI,MAAM,CAAC,IAAI,CAAC,CAGlC;IACM,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,cAAc,CAAC,KAAK,EAAE,IAAI,CAAC;IAsB5D;;;;;;;;;;;;;;;;OAgBG;IACH,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI;IAS5C;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,MAAM,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,MAAM,IAAI;IAoCxF;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC;IASzF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCG;IACH,IAAI,CAAC,MAAM,SAAS,MAAM,CAAC,GAAG,CAAC,EAAE,WAAW,EAAE,CAAC,MAAM,EAAE,IAAI,KAAK,MAAM,GAAG,MAAM;CAGhF"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Stream } from "../stream.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Adaptive filter transformer that maintains state and can terminate streams.
|
|
4
|
+
*
|
|
5
|
+
* @template VALUE - The type of values flowing through the stream
|
|
6
|
+
* @template STATE - The type of the internal state object
|
|
7
|
+
*
|
|
8
|
+
* @param initialState - Initial state object for the transformer
|
|
9
|
+
* @param predicate - Function that determines if a value should pass through
|
|
10
|
+
* - Returns `[boolean, newState]` to continue with updated state
|
|
11
|
+
* - Returns `void` or `undefined` to terminate the stream
|
|
12
|
+
* - Can be async for complex filtering logic
|
|
13
|
+
*
|
|
14
|
+
* @returns A transformer function that can be used with `.pipe()`
|
|
15
|
+
*
|
|
16
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Simple filtering
|
|
20
|
+
* stream.pipe(filter({}, (_, value) => [value > 0, {}]))
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Async filtering
|
|
24
|
+
* stream.pipe(
|
|
25
|
+
* filter({}, async (_, value) => {
|
|
26
|
+
* const valid = await validate(value);
|
|
27
|
+
* return [valid, {}];
|
|
28
|
+
* })
|
|
29
|
+
* )
|
|
30
|
+
*
|
|
31
|
+
|
|
32
|
+
*
|
|
33
|
+
*/
|
|
34
|
+
export declare function filter<VALUE, STATE extends Record<string, unknown> = {}>(initialState: STATE, predicate: (state: STATE, value: VALUE) => [boolean, STATE] | void | Promise<[boolean, STATE] | void>): (stream: Stream<VALUE>) => Stream<VALUE>;
|
|
35
|
+
//# sourceMappingURL=filter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filter.d.ts","sourceRoot":"","sources":["../../src/transformers/filter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,EAAE,EACtE,YAAY,EAAE,KAAK,EACnB,SAAS,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,CAAC,OAAO,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC,GACpG,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,MAAM,CAAC,KAAK,CAAC,CAe1C"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Stream } from "../stream.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Flatten arrays in a stream, converting 1 array event into N individual events.
|
|
4
|
+
*
|
|
5
|
+
* @template VALUE - The type of values in the stream (should be arrays)
|
|
6
|
+
* @template DEPTH - The depth of flattening (0 = one level, 1 = two levels, etc.)
|
|
7
|
+
*
|
|
8
|
+
* @param depth - How many levels deep to flatten (default: 0 = one level)
|
|
9
|
+
*
|
|
10
|
+
* @returns A transformer that flattens array values into individual events
|
|
11
|
+
*
|
|
12
|
+
* @see {@link Stream} - Complete copy-paste transformers library
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Basic flattening - 1 array → N events
|
|
16
|
+
* const arrayStream = new Stream<number[]>();
|
|
17
|
+
* const individualNumbers = arrayStream.pipe(flat());
|
|
18
|
+
*
|
|
19
|
+
* arrayStream.push([1, 2, 3]); // Emits: 1, 2, 3 as separate events
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Deep flattening
|
|
23
|
+
* const deepArrays = new Stream<number[][]>();
|
|
24
|
+
* const flattened = deepArrays.pipe(flat(1)); // Flatten 2 levels
|
|
25
|
+
*
|
|
26
|
+
* deepArrays.push([[1, 2], [3, 4]]);
|
|
27
|
+
* // Emits: 1, 2, 3, 4 as separate events
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
export declare function flat<VALUE, DEPTH extends number = 0>(depth?: DEPTH): (stream: Stream<VALUE>) => Stream<FlatArray<VALUE, DEPTH>>;
|
|
31
|
+
//# sourceMappingURL=flat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flat.d.ts","sourceRoot":"","sources":["../../src/transformers/flat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAGtC;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,KAAK,SAAS,MAAM,GAAG,CAAC,EAClD,KAAK,GAAE,KAAkB,GACxB,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAe5D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/transformers/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
|