@rimbu/common 0.9.3 → 0.10.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/README.md +1 -1
- package/dist/main/async-reducer.js +559 -0
- package/dist/main/async-reducer.js.map +1 -1
- package/dist/main/reducer.js +32 -16
- package/dist/main/reducer.js.map +1 -1
- package/dist/module/async-reducer.js +445 -0
- package/dist/module/async-reducer.js.map +1 -1
- package/dist/module/reducer.js +31 -16
- package/dist/module/reducer.js.map +1 -1
- package/dist/types/async-reducer.d.ts +310 -1
- package/dist/types/reducer.d.ts +33 -17
- package/package.json +2 -5
- package/src/async-reducer.ts +663 -0
- package/src/reducer.ts +46 -26
package/dist/types/reducer.d.ts
CHANGED
|
@@ -6,15 +6,6 @@ import { CollectFun, Eq, OptLazy } from './internal';
|
|
|
6
6
|
*/
|
|
7
7
|
export declare type Reducer<I, O = I> = Reducer.Impl<I, O, unknown>;
|
|
8
8
|
export declare namespace Reducer {
|
|
9
|
-
/**
|
|
10
|
-
* Ensures that all non-primitive type use lazy initialization to prevent accidental instance sharing.
|
|
11
|
-
*/
|
|
12
|
-
type Init<T> = T extends object | (() => any) ? () => T : T;
|
|
13
|
-
/**
|
|
14
|
-
* Returns the contained value for an `Init` instance.
|
|
15
|
-
* @param init - the `Init` value container
|
|
16
|
-
*/
|
|
17
|
-
function Init<T>(init: Reducer.Init<T>): T;
|
|
18
9
|
/**
|
|
19
10
|
* The Implementation interface for a `Reducer`, which also exposes the internal state type.
|
|
20
11
|
* @typeparam I - the input value type
|
|
@@ -25,7 +16,7 @@ export declare namespace Reducer {
|
|
|
25
16
|
/**
|
|
26
17
|
* The initial state value for the reducer algorithm.
|
|
27
18
|
*/
|
|
28
|
-
readonly init:
|
|
19
|
+
readonly init: OptLazy<S>;
|
|
29
20
|
/**
|
|
30
21
|
* Returns the next state based on the given input values
|
|
31
22
|
* @param state - the current state
|
|
@@ -54,6 +45,7 @@ export declare namespace Reducer {
|
|
|
54
45
|
filterInput(pred: (value: I, index: number, halt: () => void) => boolean): Reducer<I, O>;
|
|
55
46
|
/**
|
|
56
47
|
* Returns a `Reducer` instance that converts its input values using given `mapFun` before passing them to the reducer.
|
|
48
|
+
* @typeparam I2 - the resulting reducer input type
|
|
57
49
|
* @param mapFun - a function that returns a new value to pass to the reducer based on the following inputs:<br/>
|
|
58
50
|
* - value: the current input value<br/>
|
|
59
51
|
* - index: the current input index
|
|
@@ -66,6 +58,7 @@ export declare namespace Reducer {
|
|
|
66
58
|
mapInput<I2>(mapFun: (value: I2, index: number) => I): Reducer<I2, O>;
|
|
67
59
|
/**
|
|
68
60
|
* Returns a `Reducer` instance that converts or filters its input values using given `collectFun` before passing them to the reducer.
|
|
61
|
+
* @typeparam I2 - the resulting reducer input type
|
|
69
62
|
* @param collectFun - a function receiving<br/>
|
|
70
63
|
* - `value`: the next value<br/>
|
|
71
64
|
* - `index`: the value index<br/>
|
|
@@ -81,6 +74,7 @@ export declare namespace Reducer {
|
|
|
81
74
|
collectInput<I2>(collectFun: CollectFun<I2, I>): Reducer<I2, O>;
|
|
82
75
|
/**
|
|
83
76
|
* Returns a `Reducer` instance that converts its output values using given `mapFun`.
|
|
77
|
+
* @typeparam O2 - the resulting reducer output type
|
|
84
78
|
* @param mapFun - a function that takes the current output value and converts it to a new output value
|
|
85
79
|
* @example
|
|
86
80
|
* ```ts
|
|
@@ -128,10 +122,10 @@ export declare namespace Reducer {
|
|
|
128
122
|
* @typeparam S - the internal state type
|
|
129
123
|
*/
|
|
130
124
|
class Base<I, O, S> implements Reducer.Impl<I, O, S> {
|
|
131
|
-
readonly init:
|
|
125
|
+
readonly init: OptLazy<S>;
|
|
132
126
|
readonly next: (state: S, elem: I, index: number, halt: () => void) => S;
|
|
133
127
|
readonly stateToResult: (state: S) => O;
|
|
134
|
-
constructor(init:
|
|
128
|
+
constructor(init: OptLazy<S>, next: (state: S, elem: I, index: number, halt: () => void) => S, stateToResult: (state: S) => O);
|
|
135
129
|
filterInput(pred: (value: I, index: number, halt: () => void) => boolean): Reducer<I, O>;
|
|
136
130
|
mapInput<I2>(mapFun: (value: I2, index: number) => I): Reducer<I2, O>;
|
|
137
131
|
collectInput<I2>(collectFun: CollectFun<I2, I>): Reducer<I2, O>;
|
|
@@ -164,7 +158,7 @@ export declare namespace Reducer {
|
|
|
164
158
|
* // => 'even'
|
|
165
159
|
* ```
|
|
166
160
|
*/
|
|
167
|
-
function create<I, O = I, S = O>(init:
|
|
161
|
+
function create<I, O = I, S = O>(init: OptLazy<S>, next: (current: S, next: I, index: number, halt: () => void) => S, stateToResult: (state: S) => O): Reducer<I, O>;
|
|
168
162
|
/**
|
|
169
163
|
* Returns a `Reducer` of which the input, state, and output types are the same.
|
|
170
164
|
* @param init - the initial state value
|
|
@@ -187,7 +181,7 @@ export declare namespace Reducer {
|
|
|
187
181
|
* // => 9
|
|
188
182
|
* ```
|
|
189
183
|
*/
|
|
190
|
-
function createMono<T>(init:
|
|
184
|
+
function createMono<T>(init: OptLazy<T>, next: (current: T, next: T, index: number, halt: () => void) => T, stateToResult?: (state: T) => T): Reducer<T>;
|
|
191
185
|
/**
|
|
192
186
|
* Returns a `Reducer` of which the state and output types are the same.
|
|
193
187
|
* @param init - the initial state value
|
|
@@ -211,7 +205,7 @@ export declare namespace Reducer {
|
|
|
211
205
|
* // => 'TFT'
|
|
212
206
|
* ```
|
|
213
207
|
*/
|
|
214
|
-
function createOutput<I, O = I>(init:
|
|
208
|
+
function createOutput<I, O = I>(init: OptLazy<O>, next: (current: O, next: I, index: number, halt: () => void) => O, stateToResult?: (state: O) => O): Reducer<I, O>;
|
|
215
209
|
/**
|
|
216
210
|
* A `Reducer` that sums all given numeric input values.
|
|
217
211
|
* @example
|
|
@@ -385,8 +379,8 @@ export declare namespace Reducer {
|
|
|
385
379
|
* @typeparam O - the fallback value type
|
|
386
380
|
* @example
|
|
387
381
|
* ```ts
|
|
388
|
-
* console.log(Stream.range{ amount: 10 }).reduce(Reducer.
|
|
389
|
-
* // =>
|
|
382
|
+
* console.log(Stream.range{ amount: 10 }).reduce(Reducer.last())
|
|
383
|
+
* // => 9
|
|
390
384
|
* ```
|
|
391
385
|
*/
|
|
392
386
|
const last: {
|
|
@@ -395,6 +389,7 @@ export declare namespace Reducer {
|
|
|
395
389
|
};
|
|
396
390
|
/**
|
|
397
391
|
* Returns a `Reducer` that ouputs false as long as no input value satisfies given `pred`, true otherwise.
|
|
392
|
+
* @typeparam T - the element type
|
|
398
393
|
* @param pred - a function taking an input value and its index, and returning true if the value satisfies the predicate
|
|
399
394
|
* @example
|
|
400
395
|
* ```ts
|
|
@@ -405,6 +400,7 @@ export declare namespace Reducer {
|
|
|
405
400
|
function some<T>(pred: (value: T, index: number) => boolean): Reducer<T, boolean>;
|
|
406
401
|
/**
|
|
407
402
|
* Returns a `Reducer` that ouputs true as long as all input values satisfy the given `pred`, false otherwise.
|
|
403
|
+
* @typeparam T - the element type
|
|
408
404
|
* @param pred - a function taking an input value and its index, and returning true if the value satisfies the predicate
|
|
409
405
|
* @example
|
|
410
406
|
* ```ts
|
|
@@ -415,6 +411,7 @@ export declare namespace Reducer {
|
|
|
415
411
|
function every<T>(pred: (value: T, index: number) => boolean): Reducer<T, boolean>;
|
|
416
412
|
/**
|
|
417
413
|
* Returns a `Reducer` that outputs false as long as the given `elem` has not been encountered in the input values, true otherwise.
|
|
414
|
+
* @typeparam T - the element type
|
|
418
415
|
* @param elem - the element to search for
|
|
419
416
|
* @param eq - (optional) a comparison function that returns true if te two given input values are considered equal
|
|
420
417
|
* @example
|
|
@@ -462,6 +459,7 @@ export declare namespace Reducer {
|
|
|
462
459
|
const nonEmpty: Reducer<any, boolean>;
|
|
463
460
|
/**
|
|
464
461
|
* Returns a `Reducer` that collects received input values in an array, and returns a copy of that array as an output value when requested.
|
|
462
|
+
* @typeparam T - the element type
|
|
465
463
|
* @example
|
|
466
464
|
* ```ts
|
|
467
465
|
* console.log(Stream.of(1, 2, 3).reduce(Reducer.toArray()))
|
|
@@ -472,6 +470,8 @@ export declare namespace Reducer {
|
|
|
472
470
|
/**
|
|
473
471
|
* Returns a `Reducer` that collects received input tuples into a mutable JS Map, and returns
|
|
474
472
|
* a copy of that map when output is requested.
|
|
473
|
+
* @typeparam K - the map key type
|
|
474
|
+
* @typeparam V - the map value type
|
|
475
475
|
* @example
|
|
476
476
|
* ```ts
|
|
477
477
|
* console.log(Stream.of([1, 'a'], [2, 'b']).reduce(Reducer.toJSMap()))
|
|
@@ -482,6 +482,7 @@ export declare namespace Reducer {
|
|
|
482
482
|
/**
|
|
483
483
|
* Returns a `Reducer` that collects received input values into a mutable JS Set, and returns
|
|
484
484
|
* a copy of that map when output is requested.
|
|
485
|
+
* @typeparam T - the element type
|
|
485
486
|
* @example
|
|
486
487
|
* ```ts
|
|
487
488
|
* console.log(Stream.of(1, 2, 3).reduce(Reducer.toJSSet()))
|
|
@@ -489,6 +490,21 @@ export declare namespace Reducer {
|
|
|
489
490
|
* ```
|
|
490
491
|
*/
|
|
491
492
|
function toJSSet<T>(): Reducer<T, Set<T>>;
|
|
493
|
+
/**
|
|
494
|
+
* Returns a `Reducer` that collects 2-tuples containing keys and values into a plain JS object, and
|
|
495
|
+
* returns a copy of that object when output is requested.
|
|
496
|
+
* @typeparam K - the result object key type
|
|
497
|
+
* @typeparam V - the result object value type
|
|
498
|
+
* @example
|
|
499
|
+
* ```ts
|
|
500
|
+
* console.log(Stream.of(['a', 1], ['b', true]).reduce(Reducer.toJSObject()))
|
|
501
|
+
* // { a: 1, b: true }
|
|
502
|
+
* ```
|
|
503
|
+
*/
|
|
504
|
+
function toJSObject<K extends string | number | symbol, V>(): Reducer<[
|
|
505
|
+
K,
|
|
506
|
+
V
|
|
507
|
+
], Record<K, V>>;
|
|
492
508
|
/**
|
|
493
509
|
* Returns a `Reducer` that combines multiple input `reducers` by providing input values to all of them and collecting the outputs in an array.
|
|
494
510
|
* @param reducers - 2 or more reducers to combine
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rimbu/common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "Common types and objects used in many other Rimbu packages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"common",
|
|
@@ -60,8 +60,5 @@
|
|
|
60
60
|
"index": "src/index.ts",
|
|
61
61
|
"replacer": "../../config/denoify-rimbu-replacer.js"
|
|
62
62
|
},
|
|
63
|
-
"
|
|
64
|
-
"tslib": "^2.4.0"
|
|
65
|
-
},
|
|
66
|
-
"gitHead": "4efaf8c469d606381517984436383fd6b1b61ec0"
|
|
63
|
+
"gitHead": "9e7cd56ce066f7903c5749110c2a7a8b06809403"
|
|
67
64
|
}
|