@rimbu/common 0.8.2 → 0.9.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 +26 -15
- package/dist/main/async-optlazy.js +6 -2
- package/dist/main/async-optlazy.js.map +1 -1
- package/dist/main/async-reducer.js +22 -22
- package/dist/main/async-reducer.js.map +1 -1
- package/dist/main/collect.js +1 -1
- package/dist/main/comp.js +29 -6
- package/dist/main/comp.js.map +1 -1
- package/dist/main/eq.js +27 -1
- package/dist/main/eq.js.map +1 -1
- package/dist/main/err.js +3 -1
- package/dist/main/err.js.map +1 -1
- package/dist/main/index-range.js +1 -1
- package/dist/main/index-range.js.map +1 -1
- package/dist/main/index.js +6 -1
- package/dist/main/index.js.map +1 -1
- package/dist/main/internal.js +13 -13
- package/dist/main/internal.js.map +1 -1
- package/dist/main/optlazy.js +4 -0
- package/dist/main/optlazy.js.map +1 -1
- package/dist/main/range.js.map +1 -1
- package/dist/main/reducer.js +75 -19
- package/dist/main/reducer.js.map +1 -1
- package/dist/main/update.js +3 -1
- package/dist/main/update.js.map +1 -1
- package/dist/module/async-optlazy.js +4 -0
- package/dist/module/async-optlazy.js.map +1 -1
- package/dist/module/async-reducer.js +12 -12
- package/dist/module/async-reducer.js.map +1 -1
- package/dist/module/collect.js +1 -1
- package/dist/module/comp.js +28 -5
- package/dist/module/comp.js.map +1 -1
- package/dist/module/eq.js +26 -0
- package/dist/module/eq.js.map +1 -1
- package/dist/module/err.js +2 -0
- package/dist/module/err.js.map +1 -1
- package/dist/module/index-range.js.map +1 -1
- package/dist/module/index.js +5 -0
- package/dist/module/index.js.map +1 -1
- package/dist/module/optlazy.js +4 -0
- package/dist/module/optlazy.js.map +1 -1
- package/dist/module/range.js.map +1 -1
- package/dist/module/reducer.js +75 -19
- package/dist/module/reducer.js.map +1 -1
- package/dist/module/update.js +3 -1
- package/dist/module/update.js.map +1 -1
- package/dist/types/async-optlazy.d.ts +4 -0
- package/dist/types/async-reducer.d.ts +37 -22
- package/dist/types/collect.d.ts +2 -2
- package/dist/types/comp.d.ts +32 -5
- package/dist/types/eq.d.ts +26 -0
- package/dist/types/err.d.ts +2 -0
- package/dist/types/index-range.d.ts +14 -13
- package/dist/types/index.d.ts +5 -0
- package/dist/types/optlazy.d.ts +4 -0
- package/dist/types/range.d.ts +11 -10
- package/dist/types/reducer.d.ts +100 -29
- package/dist/types/types.d.ts +0 -4
- package/dist/types/update.d.ts +3 -1
- package/package.json +6 -4
- package/src/async-optlazy.ts +4 -0
- package/src/async-reducer.ts +37 -22
- package/src/collect.ts +2 -2
- package/src/comp.ts +40 -13
- package/src/eq.ts +26 -0
- package/src/err.ts +2 -0
- package/src/index-range.ts +14 -13
- package/src/index.ts +6 -0
- package/src/optlazy.ts +4 -0
- package/src/range.ts +11 -10
- package/src/reducer.ts +107 -31
- package/src/types.ts +0 -5
- package/src/update.ts +3 -1
package/src/optlazy.ts
CHANGED
|
@@ -8,9 +8,11 @@ export type OptLazy<T> = T | (() => T);
|
|
|
8
8
|
* Returns the value contained in an `OptLazy` instance of type T.
|
|
9
9
|
* @param optLazy - the `OptLazy` value of type T
|
|
10
10
|
* @example
|
|
11
|
+
* ```ts
|
|
11
12
|
* OptLazy(1) // => 1
|
|
12
13
|
* OptLazy(() => 1) // => 1
|
|
13
14
|
* OptLazy(() => () => 1) // => () => 1
|
|
15
|
+
* ```
|
|
14
16
|
*/
|
|
15
17
|
export function OptLazy<T>(optLazy: OptLazy<T>): T {
|
|
16
18
|
if (optLazy instanceof Function) return optLazy();
|
|
@@ -31,9 +33,11 @@ export type OptLazyOr<T, O> = T | ((none: O) => T | O);
|
|
|
31
33
|
* @param optLazyOr - a value or a function returning a value or otherwise the received value
|
|
32
34
|
* @param otherValue - the value to return if the optLazyOr does not return its own value
|
|
33
35
|
* @example
|
|
36
|
+
* ```ts
|
|
34
37
|
* OptLazyOr(1, 'a') // => 1
|
|
35
38
|
* OptLazyOr(() => 1, 'a') // => 1
|
|
36
39
|
* OptLazyOr((none) => none, 'a') // => 'a'
|
|
40
|
+
* ```
|
|
37
41
|
*/
|
|
38
42
|
export function OptLazyOr<T, O>(
|
|
39
43
|
optLazyOr: OptLazyOr<T, O>,
|
package/src/range.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A range definition for any type of (orderable) value.
|
|
3
3
|
* If a start or end is defined, a tuple can be used where the second item is a boolean
|
|
4
|
-
* indicating whether that end is inclusive or exclusive
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* - { end:
|
|
8
|
-
* - {
|
|
9
|
-
* - { start: T
|
|
10
|
-
* - { start: T, end:
|
|
11
|
-
* - { start: [T, boolean] }
|
|
12
|
-
* - { start: [T, boolean]
|
|
13
|
-
* - { start: [T, boolean], end:
|
|
4
|
+
* indicating whether that end is inclusive (true) or exclusive (false).<br/>
|
|
5
|
+
* A Range of type T can have one of the following forms:<br/>
|
|
6
|
+
* <br/>
|
|
7
|
+
* - { end: T }<br/>
|
|
8
|
+
* - { end: [T, boolean] }<br/>
|
|
9
|
+
* - { start: T }<br/>
|
|
10
|
+
* - { start: T, end: T }<br/>
|
|
11
|
+
* - { start: T, end: [T, boolean] }<br/>
|
|
12
|
+
* - { start: [T, boolean] }<br/>
|
|
13
|
+
* - { start: [T, boolean], end: T }<br/>
|
|
14
|
+
* - { start: [T, boolean], end: [T, boolean] }<br/>
|
|
14
15
|
*/
|
|
15
16
|
export type Range<T> =
|
|
16
17
|
| { start: T | [T, boolean]; end?: T | [T, boolean]; amount?: undefined }
|
package/src/reducer.ts
CHANGED
|
@@ -53,61 +53,74 @@ export namespace Reducer {
|
|
|
53
53
|
stateToResult(state: S): O;
|
|
54
54
|
/**
|
|
55
55
|
* Returns a `Reducer` instance that only passes values to the reducer that satisy the given `pred` predicate.
|
|
56
|
-
* @param pred - a function that returns true if the value should be passed to the reducer based on the following inputs
|
|
57
|
-
* - value: the current input value
|
|
58
|
-
* - index: the current input index
|
|
56
|
+
* @param pred - a function that returns true if the value should be passed to the reducer based on the following inputs:<br/>
|
|
57
|
+
* - value: the current input value<br/>
|
|
58
|
+
* - index: the current input index<br/>
|
|
59
59
|
* - halt: function that, when called, ensures no more new values are passed to the reducer
|
|
60
60
|
* @example
|
|
61
|
+
* ```ts
|
|
61
62
|
* Reducer.sum.filterInput(v => v > 10)
|
|
62
63
|
* // this reducer will only sum values larger than 10
|
|
64
|
+
* ```
|
|
63
65
|
*/
|
|
64
66
|
filterInput(
|
|
65
67
|
pred: (value: I, index: number, halt: () => void) => boolean
|
|
66
68
|
): Reducer<I, O>;
|
|
67
69
|
/**
|
|
68
70
|
* Returns a `Reducer` instance that converts its input values using given `mapFun` before passing them to the reducer.
|
|
69
|
-
* @param mapFun - a function that returns a new value to pass to the reducer based on the following inputs
|
|
70
|
-
* - value: the current input value
|
|
71
|
+
* @param mapFun - a function that returns a new value to pass to the reducer based on the following inputs:<br/>
|
|
72
|
+
* - value: the current input value<br/>
|
|
71
73
|
* - index: the current input index
|
|
72
74
|
* @example
|
|
75
|
+
* ```ts
|
|
73
76
|
* Reducer.sum.mapInput(v => v * 2)
|
|
74
77
|
* // this reducer will double all input values before summing them
|
|
78
|
+
* ```
|
|
75
79
|
*/
|
|
76
80
|
mapInput<I2>(mapFun: (value: I2, index: number) => I): Reducer<I2, O>;
|
|
77
81
|
/**
|
|
78
82
|
* Returns a `Reducer` instance that converts or filters its input values using given `collectFun` before passing them to the reducer.
|
|
79
|
-
* @param collectFun - a function receiving
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
83
|
+
* @param collectFun - a function receiving<br/>
|
|
84
|
+
* - `value`: the next value<br/>
|
|
85
|
+
* - `index`: the value index<br/>
|
|
86
|
+
* - `skip`: a token that, when returned, will not add a value to the resulting collection<br/>
|
|
87
|
+
* - `halt`: a function that, when called, ensures no next elements are passed
|
|
84
88
|
* @example
|
|
89
|
+
* ```ts
|
|
85
90
|
* Reducer.sum.collectInput((v, _, skip) => v <= 10 ? skip : v * 2)
|
|
86
91
|
* // this reducer will double all input values larger thant 10 before summing them,
|
|
87
92
|
* // and will skip all values smaller than 10
|
|
93
|
+
* ```
|
|
88
94
|
*/
|
|
89
95
|
collectInput<I2>(collectFun: CollectFun<I2, I>): Reducer<I2, O>;
|
|
90
96
|
/**
|
|
91
97
|
* Returns a `Reducer` instance that converts its output values using given `mapFun`.
|
|
92
98
|
* @param mapFun - a function that takes the current output value and converts it to a new output value
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
93
101
|
* Reducer.sum.mapOutput(String)
|
|
94
102
|
* // this reducer will convert all its results to string before returning them
|
|
103
|
+
* ```
|
|
95
104
|
*/
|
|
96
105
|
mapOutput<O2>(mapFun: (value: O) => O2): Reducer<I, O2>;
|
|
97
106
|
/**
|
|
98
107
|
* Returns a `Reducer` instance that takes at most the given `amount` of input elements, and will ignore subsequent elements.
|
|
99
108
|
* @param amount - the amount of elements to accept
|
|
100
109
|
* @example
|
|
110
|
+
* ```ts
|
|
101
111
|
* Stream.range({ end: 10 }).reduce(Reducer.sum.takeInput(2))
|
|
102
112
|
* // => 1
|
|
113
|
+
* ```
|
|
103
114
|
*/
|
|
104
115
|
takeInput(amount: number): Reducer<I, O>;
|
|
105
116
|
/**
|
|
106
117
|
* Returns a `Reducer` instance that skips the first given `amount` of input elements, and will process subsequent elements.
|
|
107
118
|
* @param amount - the amount of elements to skip
|
|
108
119
|
* @example
|
|
120
|
+
* ```ts
|
|
109
121
|
* Stream.range({ end: 10 }).reduce(Reducer.sum.dropInput(9))
|
|
110
122
|
* // => 19
|
|
123
|
+
* ```
|
|
111
124
|
*/
|
|
112
125
|
dropInput(amount: number): Reducer<I, O>;
|
|
113
126
|
/**
|
|
@@ -115,8 +128,10 @@ export namespace Reducer {
|
|
|
115
128
|
* @param from - (default: 0) the index at which to start processing elements
|
|
116
129
|
* @param amount - (optional) the amount of elements to process, if not given, processes all elements from the `from` index
|
|
117
130
|
* @example
|
|
131
|
+
* ```ts
|
|
118
132
|
* Stream.range({ end: 10 }).reduce(Reducer.sum.sliceInput(1, 2))
|
|
119
133
|
* // => 3
|
|
134
|
+
* ```
|
|
120
135
|
*/
|
|
121
136
|
sliceInput(from?: number, amount?: number): Reducer<I, O>;
|
|
122
137
|
}
|
|
@@ -222,16 +237,17 @@ export namespace Reducer {
|
|
|
222
237
|
/**
|
|
223
238
|
* Returns a `Reducer` with the given options:
|
|
224
239
|
* @param init - the initial state value
|
|
225
|
-
* @param next - returns the next state value based on the given inputs
|
|
226
|
-
* - current: the current state
|
|
227
|
-
* - next: the current input value
|
|
228
|
-
* - index: the input index value
|
|
240
|
+
* @param next - returns the next state value based on the given inputs:<br/>
|
|
241
|
+
* - current: the current state<br/>
|
|
242
|
+
* - next: the current input value<br/>
|
|
243
|
+
* - index: the input index value<br/>
|
|
229
244
|
* - halt: function that, when called, ensures no more elements are passed to the reducer
|
|
230
245
|
* @param stateToResult - a function that converts the current state to an output value
|
|
231
246
|
* @typeparam I - the input value type
|
|
232
247
|
* @typeparam O - the output value type
|
|
233
248
|
* @typeparam S - the internal state type
|
|
234
249
|
* @example
|
|
250
|
+
* ```ts
|
|
235
251
|
* const evenNumberOfOnes = Reducer
|
|
236
252
|
* .create(
|
|
237
253
|
* true,
|
|
@@ -240,6 +256,7 @@ export namespace Reducer {
|
|
|
240
256
|
* const result = Stream.of(1, 2, 3, 2, 1)).reduce(evenNumberOfOnes)
|
|
241
257
|
* console.log+(result)
|
|
242
258
|
* // => 'even'
|
|
259
|
+
* ```
|
|
243
260
|
*/
|
|
244
261
|
export function create<I, O = I, S = O>(
|
|
245
262
|
init: Reducer.Init<S>,
|
|
@@ -252,13 +269,15 @@ export namespace Reducer {
|
|
|
252
269
|
/**
|
|
253
270
|
* Returns a `Reducer` of which the input, state, and output types are the same.
|
|
254
271
|
* @param init - the initial state value
|
|
255
|
-
* @param next - returns the next state value based on the given inputs
|
|
256
|
-
* - current: the current state
|
|
257
|
-
* - next: the current input value
|
|
258
|
-
* - index: the input index value
|
|
272
|
+
* @param next - returns the next state value based on the given inputs:<br/>
|
|
273
|
+
* - current: the current state<br/>
|
|
274
|
+
* - next: the current input value<br/>
|
|
275
|
+
* - index: the input index value<br/>
|
|
259
276
|
* - halt: function that, when called, ensures no more elements are passed to the reducer
|
|
260
277
|
* @param stateToResult - (optional) a function that converts the current state to an output value
|
|
261
278
|
* @typeparam T - the overall value type
|
|
279
|
+
* @example
|
|
280
|
+
* ```ts
|
|
262
281
|
* const sum = Reducer
|
|
263
282
|
* .createMono(
|
|
264
283
|
* 0,
|
|
@@ -267,6 +286,7 @@ export namespace Reducer {
|
|
|
267
286
|
* const result = Stream.of(1, 2, 3, 2, 1)).reduce(sum)
|
|
268
287
|
* console.log+(result)
|
|
269
288
|
* // => 9
|
|
289
|
+
* ```
|
|
270
290
|
*/
|
|
271
291
|
export function createMono<T>(
|
|
272
292
|
init: Reducer.Init<T>,
|
|
@@ -279,14 +299,16 @@ export namespace Reducer {
|
|
|
279
299
|
/**
|
|
280
300
|
* Returns a `Reducer` of which the state and output types are the same.
|
|
281
301
|
* @param init - the initial state value
|
|
282
|
-
* @param next - returns the next state value based on the given inputs
|
|
283
|
-
* - current: the current state
|
|
284
|
-
* - next: the current input value
|
|
285
|
-
* - index: the input index value
|
|
302
|
+
* @param next - returns the next state value based on the given inputs:<br/>
|
|
303
|
+
* - current: the current state<br/>
|
|
304
|
+
* - next: the current input value<br/>
|
|
305
|
+
* - index: the input index value<br/>
|
|
286
306
|
* - halt: function that, when called, ensures no more elements are passed to the reducer
|
|
287
307
|
* @param stateToResult - (optional) a function that converts the current state to an output value
|
|
288
308
|
* @typeparam I - the input value type
|
|
289
309
|
* @typeparam O - the output value type
|
|
310
|
+
* @example
|
|
311
|
+
* ```ts
|
|
290
312
|
* const boolToString = Reducer
|
|
291
313
|
* .createOutput(
|
|
292
314
|
* '',
|
|
@@ -295,6 +317,7 @@ export namespace Reducer {
|
|
|
295
317
|
* const result = Stream.of(true, false, true)).reduce(boolToString)
|
|
296
318
|
* console.log+(result)
|
|
297
319
|
* // => 'TFT'
|
|
320
|
+
* ```
|
|
298
321
|
*/
|
|
299
322
|
export function createOutput<I, O = I>(
|
|
300
323
|
init: Reducer.Init<O>,
|
|
@@ -307,16 +330,20 @@ export namespace Reducer {
|
|
|
307
330
|
/**
|
|
308
331
|
* A `Reducer` that sums all given numeric input values.
|
|
309
332
|
* @example
|
|
333
|
+
* ```ts
|
|
310
334
|
* console.log(Stream.range({ amount: 5 }).reduce(Reducer.sum))
|
|
311
335
|
* // => 10
|
|
336
|
+
* ```
|
|
312
337
|
*/
|
|
313
338
|
export const sum = createMono(0, (state, next): number => state + next);
|
|
314
339
|
|
|
315
340
|
/**
|
|
316
341
|
* A `Reducer` that calculates the product of all given numeric input values.
|
|
317
342
|
* @example
|
|
343
|
+
* ```ts
|
|
318
344
|
* console.log(Stream.range({ start: 1, amount: 5 }).reduce(product))
|
|
319
345
|
* // => 120
|
|
346
|
+
* ```
|
|
320
347
|
*/
|
|
321
348
|
export const product = createMono(1, (state, next, _, halt): number => {
|
|
322
349
|
if (0 === next) halt();
|
|
@@ -326,8 +353,10 @@ export namespace Reducer {
|
|
|
326
353
|
/**
|
|
327
354
|
* A `Reducer` that calculates the average of all given numberic input values.
|
|
328
355
|
* @example
|
|
356
|
+
* ```ts
|
|
329
357
|
* console.log(Stream.range({ amount: 5 }).reduce(Reducer.average));
|
|
330
358
|
* // => 2
|
|
359
|
+
* ```
|
|
331
360
|
*/
|
|
332
361
|
export const average = createMono(
|
|
333
362
|
0,
|
|
@@ -339,9 +368,11 @@ export namespace Reducer {
|
|
|
339
368
|
* @param compFun - a comparison function for two input values, returning 0 when equal, positive when greater, negetive when smaller
|
|
340
369
|
* @param otherwise - (default: undefineds) a fallback value when there were no input values given
|
|
341
370
|
* @example
|
|
371
|
+
* ```ts
|
|
342
372
|
* const stream = Stream.of('abc', 'a', 'abcde', 'ab')
|
|
343
373
|
* console.log(stream.minBy((s1, s2) => s1.length - s2.length))
|
|
344
374
|
* // 'a'
|
|
375
|
+
* ```
|
|
345
376
|
*/
|
|
346
377
|
export const minBy: {
|
|
347
378
|
<T>(compFun: (v1: T, v2: T) => number): Reducer<T, T | undefined>;
|
|
@@ -365,8 +396,10 @@ export namespace Reducer {
|
|
|
365
396
|
* Returns a `Reducer` that remembers the minimum value of the numberic inputs.
|
|
366
397
|
* @param otherwise - (default: undefined) a fallback value when there were no input values given
|
|
367
398
|
* @example
|
|
399
|
+
* ```ts
|
|
368
400
|
* console.log(Stream.of(5, 3, 7, 4).reduce(Reducer.min()))
|
|
369
401
|
* // => 3
|
|
402
|
+
* ```
|
|
370
403
|
*/
|
|
371
404
|
export const min: {
|
|
372
405
|
(): Reducer<number, number | undefined>;
|
|
@@ -385,9 +418,11 @@ export namespace Reducer {
|
|
|
385
418
|
* @param compFun - a comparison function for two input values, returning 0 when equal, positive when greater, negetive when smaller
|
|
386
419
|
* @param otherwise - (default: undefined) a fallback value when there were no input values given
|
|
387
420
|
* @example
|
|
421
|
+
* ```ts
|
|
388
422
|
* const stream = Stream.of('abc', 'a', 'abcde', 'ab')
|
|
389
423
|
* console.log(stream.maxBy((s1, s2) => s1.length - s2.length))
|
|
390
424
|
* // 'abcde'
|
|
425
|
+
* ```
|
|
391
426
|
*/
|
|
392
427
|
export const maxBy: {
|
|
393
428
|
<T>(compFun: (v1: T, v2: T) => number): Reducer<T, T | undefined>;
|
|
@@ -414,8 +449,10 @@ export namespace Reducer {
|
|
|
414
449
|
* Returns a `Reducer` that remembers the maximum value of the numberic inputs.
|
|
415
450
|
* @param otherwise - (default: undefined) a fallback value when there were no input values given
|
|
416
451
|
* @example
|
|
452
|
+
* ```ts
|
|
417
453
|
* console.log(Stream.of(5, 3, 7, 4).reduce(Reducer.max()))
|
|
418
454
|
* // => 7
|
|
455
|
+
* ```
|
|
419
456
|
*/
|
|
420
457
|
export const max: {
|
|
421
458
|
(): Reducer<number, number | undefined>;
|
|
@@ -430,14 +467,16 @@ export namespace Reducer {
|
|
|
430
467
|
};
|
|
431
468
|
|
|
432
469
|
/**
|
|
433
|
-
* Returns a `
|
|
434
|
-
* @options - an object containing
|
|
435
|
-
* - sep: (optional) a seperator string value between values in the output
|
|
436
|
-
* - start: (optional) a start string to prepend to the output
|
|
437
|
-
* - end: (optional) an end string to append to the output
|
|
470
|
+
* Returns a `yaReducer` that joins the given input values into a string using the given options.
|
|
471
|
+
* @param options - an object containing:<br/>
|
|
472
|
+
* - sep: (optional) a seperator string value between values in the output<br/>
|
|
473
|
+
* - start: (optional) a start string to prepend to the output<br/>
|
|
474
|
+
* - end: (optional) an end string to append to the output<br/>
|
|
438
475
|
* @example
|
|
476
|
+
* ```ts
|
|
439
477
|
* console.log(Stream.of(1, 2, 3).reduce(Reducer.join({ sep: '-' })))
|
|
440
478
|
* // => '1-2-3'
|
|
479
|
+
* ```
|
|
441
480
|
*/
|
|
442
481
|
export function join<T>({
|
|
443
482
|
sep = '',
|
|
@@ -461,15 +500,17 @@ export namespace Reducer {
|
|
|
461
500
|
|
|
462
501
|
/**
|
|
463
502
|
* Returns a `Reducer` that remembers the amount of input items provided.
|
|
464
|
-
* @param pred - (optional) a predicate that returns false if the item should not be counted given
|
|
465
|
-
* - value: the current input value
|
|
503
|
+
* @param pred - (optional) a predicate that returns false if the item should not be counted given:<br/>
|
|
504
|
+
* - value: the current input value<br/>
|
|
466
505
|
* - index: the input value index
|
|
467
506
|
* @example
|
|
507
|
+
* ```ts
|
|
468
508
|
* const stream = Stream.range({ amount: 10 })
|
|
469
509
|
* console.log(stream.reduce(Reducer.count()))
|
|
470
510
|
* // => 10
|
|
471
511
|
* console.log(stream.reduce(Reducer.count(v => v < 5)))
|
|
472
512
|
* // => 5
|
|
513
|
+
* ```
|
|
473
514
|
*/
|
|
474
515
|
export const count: {
|
|
475
516
|
(): Reducer<any, number>;
|
|
@@ -490,8 +531,10 @@ export namespace Reducer {
|
|
|
490
531
|
* @typeparam T - the input value type
|
|
491
532
|
* @typeparam O - the fallback value type
|
|
492
533
|
* @example
|
|
534
|
+
* ```ts
|
|
493
535
|
* console.log(Stream.range({ amount: 10 }).reduce(Reducer.firstWhere(v => v > 5)))
|
|
494
536
|
* // => 6
|
|
537
|
+
* ```
|
|
495
538
|
*/
|
|
496
539
|
export const firstWhere: {
|
|
497
540
|
<T>(pred: (value: T, index: number) => boolean): Reducer<T, T | undefined>;
|
|
@@ -524,8 +567,10 @@ export namespace Reducer {
|
|
|
524
567
|
* @typeparam T - the input value type
|
|
525
568
|
* @typeparam O - the fallback value type
|
|
526
569
|
* @example
|
|
570
|
+
* ```ts
|
|
527
571
|
* console.log(Stream.range{ amount: 10 }).reduce(Reducer.first())
|
|
528
572
|
* // => 0
|
|
573
|
+
* ```
|
|
529
574
|
*/
|
|
530
575
|
export const first: {
|
|
531
576
|
<T>(): Reducer<T, T | undefined>;
|
|
@@ -551,8 +596,10 @@ export namespace Reducer {
|
|
|
551
596
|
* @typeparam T - the input value type
|
|
552
597
|
* @typeparam O - the fallback value type
|
|
553
598
|
* @example
|
|
599
|
+
* ```ts
|
|
554
600
|
* console.log(Stream.range({ amount: 10 }).reduce(Reducer.lastWhere(v => v > 5)))
|
|
555
601
|
* // => 9
|
|
602
|
+
* ```
|
|
556
603
|
*/
|
|
557
604
|
export const lastWhere: {
|
|
558
605
|
<T>(pred: (value: T, index: number) => boolean): Reducer<T, T | undefined>;
|
|
@@ -582,8 +629,10 @@ export namespace Reducer {
|
|
|
582
629
|
* @typeparam T - the input value type
|
|
583
630
|
* @typeparam O - the fallback value type
|
|
584
631
|
* @example
|
|
632
|
+
* ```ts
|
|
585
633
|
* console.log(Stream.range{ amount: 10 }).reduce(Reducer.first())
|
|
586
634
|
* // => 0
|
|
635
|
+
* ```
|
|
587
636
|
*/
|
|
588
637
|
export const last: {
|
|
589
638
|
<T>(): Reducer<T, T | undefined>;
|
|
@@ -602,8 +651,10 @@ export namespace Reducer {
|
|
|
602
651
|
* Returns a `Reducer` that ouputs false as long as no input value satisfies given `pred`, true otherwise.
|
|
603
652
|
* @param pred - a function taking an input value and its index, and returning true if the value satisfies the predicate
|
|
604
653
|
* @example
|
|
654
|
+
* ```ts
|
|
605
655
|
* console.log(Stream.range{ amount: 10 }).reduce(Reducer.some(v => v > 5))
|
|
606
656
|
* // => true
|
|
657
|
+
* ```
|
|
607
658
|
*/
|
|
608
659
|
export function some<T>(
|
|
609
660
|
pred: (value: T, index: number) => boolean
|
|
@@ -624,8 +675,10 @@ export namespace Reducer {
|
|
|
624
675
|
* Returns a `Reducer` that ouputs true as long as all input values satisfy the given `pred`, false otherwise.
|
|
625
676
|
* @param pred - a function taking an input value and its index, and returning true if the value satisfies the predicate
|
|
626
677
|
* @example
|
|
678
|
+
* ```ts
|
|
627
679
|
* console.log(Stream.range{ amount: 10 }).reduce(Reducer.every(v => v < 5))
|
|
628
680
|
* // => false
|
|
681
|
+
* ```
|
|
629
682
|
*/
|
|
630
683
|
export function every<T>(
|
|
631
684
|
pred: (value: T, index: number) => boolean
|
|
@@ -648,8 +701,10 @@ export namespace Reducer {
|
|
|
648
701
|
* @param elem - the element to search for
|
|
649
702
|
* @param eq - (optional) a comparison function that returns true if te two given input values are considered equal
|
|
650
703
|
* @example
|
|
704
|
+
* ```ts
|
|
651
705
|
* console.log(Stream.range({ amount: 10 }).reduce(Reducer.contains(5)))
|
|
652
706
|
* // => true
|
|
707
|
+
* ```
|
|
653
708
|
*/
|
|
654
709
|
export function contains<T>(
|
|
655
710
|
elem: T,
|
|
@@ -670,8 +725,10 @@ export namespace Reducer {
|
|
|
670
725
|
/**
|
|
671
726
|
* Returns a `Reducer` that takes boolean values and outputs true if all input values are true, and false otherwise.
|
|
672
727
|
* @example
|
|
728
|
+
* ```ts
|
|
673
729
|
* console.log(Stream.of(true, false, true)).reduce(Reducer.and))
|
|
674
730
|
* // => false
|
|
731
|
+
* ```
|
|
675
732
|
*/
|
|
676
733
|
export const and = createMono(true, (state, next, _, halt): boolean => {
|
|
677
734
|
if (!state) return state;
|
|
@@ -682,8 +739,10 @@ export namespace Reducer {
|
|
|
682
739
|
/**
|
|
683
740
|
* Returns a `Reducer` that takes boolean values and outputs true if one or more input values are true, and false otherwise.
|
|
684
741
|
* @example
|
|
742
|
+
* ```ts
|
|
685
743
|
* console.log(Stream.of(true, false, true)).reduce(Reducer.or))
|
|
686
744
|
* // => true
|
|
745
|
+
* ```
|
|
687
746
|
*/
|
|
688
747
|
export const or = createMono(false, (state, next, _, halt): boolean => {
|
|
689
748
|
if (state) return state;
|
|
@@ -694,8 +753,10 @@ export namespace Reducer {
|
|
|
694
753
|
/**
|
|
695
754
|
* Returns a `Reducer` that outputs true if no input values are received, false otherwise.
|
|
696
755
|
* @example
|
|
756
|
+
* ```ts
|
|
697
757
|
* console.log(Stream.of(1, 2, 3).reduce(Reducer.isEmpty))
|
|
698
758
|
* // => false
|
|
759
|
+
* ```
|
|
699
760
|
*/
|
|
700
761
|
export const isEmpty = createOutput<any, boolean>(
|
|
701
762
|
true,
|
|
@@ -708,8 +769,10 @@ export namespace Reducer {
|
|
|
708
769
|
/**
|
|
709
770
|
* Returns a `Reducer` that outputs true if one or more input values are received, false otherwise.
|
|
710
771
|
* @example
|
|
772
|
+
* ```ts
|
|
711
773
|
* console.log(Stream.of(1, 2, 3).reduce(Reducer.nonEmpty))
|
|
712
774
|
* // => true
|
|
775
|
+
* ```
|
|
713
776
|
*/
|
|
714
777
|
export const nonEmpty = createOutput<any, boolean>(
|
|
715
778
|
false,
|
|
@@ -722,8 +785,10 @@ export namespace Reducer {
|
|
|
722
785
|
/**
|
|
723
786
|
* Returns a `Reducer` that collects received input values in an array, and returns a copy of that array as an output value when requested.
|
|
724
787
|
* @example
|
|
788
|
+
* ```ts
|
|
725
789
|
* console.log(Stream.of(1, 2, 3).reduce(Reducer.toArray()))
|
|
726
790
|
* // => [1, 2, 3]
|
|
791
|
+
* ```
|
|
727
792
|
*/
|
|
728
793
|
export function toArray<T>(): Reducer<T, T[]> {
|
|
729
794
|
return create(
|
|
@@ -740,8 +805,10 @@ export namespace Reducer {
|
|
|
740
805
|
* Returns a `Reducer` that collects received input tuples into a mutable JS Map, and returns
|
|
741
806
|
* a copy of that map when output is requested.
|
|
742
807
|
* @example
|
|
808
|
+
* ```ts
|
|
743
809
|
* console.log(Stream.of([1, 'a'], [2, 'b']).reduce(Reducer.toJSMap()))
|
|
744
810
|
* // Map { 1 => 'a', 2 => 'b' }
|
|
811
|
+
* ```
|
|
745
812
|
*/
|
|
746
813
|
export function toJSMap<K, V>(): Reducer<[K, V], Map<K, V>> {
|
|
747
814
|
return create(
|
|
@@ -758,8 +825,10 @@ export namespace Reducer {
|
|
|
758
825
|
* Returns a `Reducer` that collects received input values into a mutable JS Set, and returns
|
|
759
826
|
* a copy of that map when output is requested.
|
|
760
827
|
* @example
|
|
828
|
+
* ```ts
|
|
761
829
|
* console.log(Stream.of(1, 2, 3).reduce(Reducer.toJSSet()))
|
|
762
830
|
* // Set {1, 2, 3}
|
|
831
|
+
* ```
|
|
763
832
|
*/
|
|
764
833
|
export function toJSSet<T>(): Reducer<T, Set<T>> {
|
|
765
834
|
return create(
|
|
@@ -776,9 +845,11 @@ export namespace Reducer {
|
|
|
776
845
|
* Returns a `Reducer` that combines multiple input `reducers` by providing input values to all of them and collecting the outputs in an array.
|
|
777
846
|
* @param reducers - 2 or more reducers to combine
|
|
778
847
|
* @example
|
|
848
|
+
* ```ts
|
|
779
849
|
* const red = Reducer.combine(Reducer.sum, Reducer.average)
|
|
780
850
|
* console.log(Stream.range({amount: 9 }).reduce(red))
|
|
781
851
|
* // => [36, 4]
|
|
852
|
+
* ```
|
|
782
853
|
*/
|
|
783
854
|
export function combine<
|
|
784
855
|
T,
|
|
@@ -786,12 +857,17 @@ export namespace Reducer {
|
|
|
786
857
|
>(
|
|
787
858
|
...reducers: { [K in keyof R]: Reducer<T, R[K]> } & Reducer<T, unknown>[]
|
|
788
859
|
): Reducer<T, R> {
|
|
789
|
-
const createState = ()
|
|
860
|
+
const createState = (): {
|
|
861
|
+
reducer: Reducer<T, unknown>;
|
|
862
|
+
halted: boolean;
|
|
863
|
+
halt(): void;
|
|
864
|
+
state: unknown;
|
|
865
|
+
}[] => {
|
|
790
866
|
return reducers.map((reducer) => {
|
|
791
867
|
const result = {
|
|
792
868
|
reducer,
|
|
793
869
|
halted: false,
|
|
794
|
-
halt() {
|
|
870
|
+
halt(): void {
|
|
795
871
|
result.halted = true;
|
|
796
872
|
},
|
|
797
873
|
state: Init(reducer.init),
|
package/src/types.ts
CHANGED
|
@@ -29,11 +29,6 @@ export type StringNonEmpty<T> = T extends string
|
|
|
29
29
|
: T
|
|
30
30
|
: never;
|
|
31
31
|
|
|
32
|
-
/**
|
|
33
|
-
* A stronger version of `Omit`, which only accepts keys that are in type T.
|
|
34
|
-
*/
|
|
35
|
-
export type OmitStrong<T, K extends keyof T> = Omit<T, K>;
|
|
36
|
-
|
|
37
32
|
/**
|
|
38
33
|
* Utility type to convert some object to a JSON serializable format.
|
|
39
34
|
* @typeparam V - the `value` type
|
package/src/update.ts
CHANGED
|
@@ -6,13 +6,15 @@ export type Update<T> = T | ((value: T) => T);
|
|
|
6
6
|
/**
|
|
7
7
|
* Returns the result of given `update` parameter, where it can either directly give a new value,
|
|
8
8
|
* or it is a function receiving the given `value`, and returns a new value.
|
|
9
|
-
* @param value - the current
|
|
9
|
+
* @param value - the current value
|
|
10
10
|
* @param update - an `Update` value, either a new value or a function receiving the old value
|
|
11
11
|
* and returning a new one.
|
|
12
12
|
* @example
|
|
13
|
+
* ```ts
|
|
13
14
|
* Update(1, 2) // => 2
|
|
14
15
|
* Update(1, () => 10) // => 10
|
|
15
16
|
* Update(1, v => v + 1) // => 2
|
|
17
|
+
* ```
|
|
16
18
|
*/
|
|
17
19
|
export function Update<T>(value: T, update: Update<T>): T {
|
|
18
20
|
if (typeof update === 'function') {
|