@rimbu/common 0.12.3 → 1.0.0-alpha.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.
Files changed (139) hide show
  1. package/README.md +21 -4
  2. package/{src/async-optlazy.ts → dist/bun/async-optlazy.mts} +1 -1
  3. package/{src/async-reducer.ts → dist/bun/async-reducer.mts} +17 -8
  4. package/{src/collect.ts → dist/bun/collect.mts} +1 -1
  5. package/{src/comp.ts → dist/bun/comp.mts} +1 -1
  6. package/{src/index-range.ts → dist/bun/index-range.mts} +1 -1
  7. package/{src/index.ts → dist/bun/index.mts} +1 -1
  8. package/dist/bun/internal.mts +13 -0
  9. package/{src/reducer.ts → dist/bun/reducer.mts} +5 -3
  10. package/dist/cjs/async-optlazy.cjs +44 -0
  11. package/dist/cjs/async-reducer.cjs +1621 -0
  12. package/dist/cjs/collect.cjs +33 -0
  13. package/dist/cjs/comp.cjs +1621 -0
  14. package/dist/cjs/eq.cjs +210 -0
  15. package/dist/cjs/err.cjs +55 -0
  16. package/dist/cjs/index-range.cjs +114 -0
  17. package/dist/cjs/index.cjs +1685 -0
  18. package/dist/cjs/internal.cjs +1685 -0
  19. package/dist/cjs/optlazy.cjs +41 -0
  20. package/dist/cjs/range.cjs +52 -0
  21. package/dist/cjs/reducer.cjs +1621 -0
  22. package/dist/cjs/traverse-state.cjs +49 -0
  23. package/dist/cjs/types.cjs +18 -0
  24. package/dist/cjs/update.cjs +35 -0
  25. package/dist/{module/async-optlazy.js → esm/async-optlazy.mjs} +5 -8
  26. package/dist/esm/async-optlazy.mjs.map +1 -0
  27. package/dist/{module/async-reducer.js → esm/async-reducer.mjs} +67 -69
  28. package/dist/esm/async-reducer.mjs.map +1 -0
  29. package/dist/{module/collect.js → esm/collect.mjs} +1 -1
  30. package/dist/esm/collect.mjs.map +1 -0
  31. package/dist/{module/comp.js → esm/comp.mjs} +2 -2
  32. package/dist/esm/comp.mjs.map +1 -0
  33. package/dist/{module/eq.js → esm/eq.mjs} +1 -1
  34. package/dist/esm/eq.mjs.map +1 -0
  35. package/dist/{module/err.js → esm/err.mjs} +1 -1
  36. package/dist/esm/err.mjs.map +1 -0
  37. package/dist/{module/index-range.js → esm/index-range.mjs} +2 -3
  38. package/dist/esm/index-range.mjs.map +1 -0
  39. package/dist/esm/index.mjs +7 -0
  40. package/dist/esm/index.mjs.map +1 -0
  41. package/dist/esm/internal.mjs +14 -0
  42. package/dist/esm/internal.mjs.map +1 -0
  43. package/dist/{module/optlazy.js → esm/optlazy.mjs} +1 -1
  44. package/dist/esm/optlazy.mjs.map +1 -0
  45. package/dist/{module/range.js → esm/range.mjs} +1 -1
  46. package/dist/esm/range.mjs.map +1 -0
  47. package/dist/{module/reducer.js → esm/reducer.mjs} +10 -8
  48. package/dist/esm/reducer.mjs.map +1 -0
  49. package/dist/{module/traverse-state.js → esm/traverse-state.mjs} +1 -1
  50. package/dist/esm/traverse-state.mjs.map +1 -0
  51. package/dist/esm/types.mjs +2 -0
  52. package/dist/esm/types.mjs.map +1 -0
  53. package/dist/{module/update.js → esm/update.mjs} +1 -1
  54. package/dist/esm/update.mjs.map +1 -0
  55. package/dist/types/{async-optlazy.d.ts → async-optlazy.d.mts} +1 -1
  56. package/dist/types/{async-reducer.d.ts → async-reducer.d.mts} +2 -2
  57. package/dist/types/{collect.d.ts → collect.d.mts} +1 -1
  58. package/dist/types/{comp.d.ts → comp.d.mts} +1 -1
  59. package/dist/types/{index-range.d.ts → index-range.d.mts} +1 -1
  60. package/dist/types/{index.d.ts → index.d.mts} +1 -1
  61. package/dist/types/internal.d.mts +13 -0
  62. package/dist/types/{reducer.d.ts → reducer.d.mts} +1 -1
  63. package/package.json +27 -21
  64. package/src/async-optlazy.mts +49 -0
  65. package/src/async-reducer.mts +1157 -0
  66. package/src/collect.mts +43 -0
  67. package/src/comp.mts +625 -0
  68. package/src/eq.mts +406 -0
  69. package/src/err.mts +43 -0
  70. package/src/index-range.mts +120 -0
  71. package/{dist/module/index.js → src/index.mts} +2 -2
  72. package/src/internal.mts +13 -0
  73. package/{dist/main/optlazy.js → src/optlazy.mts} +24 -14
  74. package/src/range.mts +58 -0
  75. package/src/reducer.mts +1025 -0
  76. package/src/traverse-state.mts +59 -0
  77. package/src/types.mts +40 -0
  78. package/{dist/main/update.js → src/update.mts} +10 -10
  79. package/dist/main/async-optlazy.js +0 -48
  80. package/dist/main/async-optlazy.js.map +0 -1
  81. package/dist/main/async-reducer.js +0 -872
  82. package/dist/main/async-reducer.js.map +0 -1
  83. package/dist/main/collect.js +0 -11
  84. package/dist/main/collect.js.map +0 -1
  85. package/dist/main/comp.js +0 -542
  86. package/dist/main/comp.js.map +0 -1
  87. package/dist/main/eq.js +0 -382
  88. package/dist/main/eq.js.map +0 -1
  89. package/dist/main/err.js +0 -62
  90. package/dist/main/err.js.map +0 -1
  91. package/dist/main/index-range.js +0 -110
  92. package/dist/main/index-range.js.map +0 -1
  93. package/dist/main/index.js +0 -10
  94. package/dist/main/index.js.map +0 -1
  95. package/dist/main/internal.js +0 -17
  96. package/dist/main/internal.js.map +0 -1
  97. package/dist/main/optlazy.js.map +0 -1
  98. package/dist/main/range.js +0 -38
  99. package/dist/main/range.js.map +0 -1
  100. package/dist/main/reducer.js +0 -694
  101. package/dist/main/reducer.js.map +0 -1
  102. package/dist/main/traverse-state.js +0 -33
  103. package/dist/main/traverse-state.js.map +0 -1
  104. package/dist/main/types.js +0 -3
  105. package/dist/main/types.js.map +0 -1
  106. package/dist/main/update.js.map +0 -1
  107. package/dist/module/async-optlazy.js.map +0 -1
  108. package/dist/module/async-reducer.js.map +0 -1
  109. package/dist/module/collect.js.map +0 -1
  110. package/dist/module/comp.js.map +0 -1
  111. package/dist/module/eq.js.map +0 -1
  112. package/dist/module/err.js.map +0 -1
  113. package/dist/module/index-range.js.map +0 -1
  114. package/dist/module/index.js.map +0 -1
  115. package/dist/module/internal.js +0 -14
  116. package/dist/module/internal.js.map +0 -1
  117. package/dist/module/optlazy.js.map +0 -1
  118. package/dist/module/range.js.map +0 -1
  119. package/dist/module/reducer.js.map +0 -1
  120. package/dist/module/traverse-state.js.map +0 -1
  121. package/dist/module/types.js +0 -2
  122. package/dist/module/types.js.map +0 -1
  123. package/dist/module/update.js.map +0 -1
  124. package/dist/types/internal.d.ts +0 -13
  125. package/src/internal.ts +0 -13
  126. /package/{src/eq.ts → dist/bun/eq.mts} +0 -0
  127. /package/{src/err.ts → dist/bun/err.mts} +0 -0
  128. /package/{src/optlazy.ts → dist/bun/optlazy.mts} +0 -0
  129. /package/{src/range.ts → dist/bun/range.mts} +0 -0
  130. /package/{src/traverse-state.ts → dist/bun/traverse-state.mts} +0 -0
  131. /package/{src/types.ts → dist/bun/types.mts} +0 -0
  132. /package/{src/update.ts → dist/bun/update.mts} +0 -0
  133. /package/dist/types/{eq.d.ts → eq.d.mts} +0 -0
  134. /package/dist/types/{err.d.ts → err.d.mts} +0 -0
  135. /package/dist/types/{optlazy.d.ts → optlazy.d.mts} +0 -0
  136. /package/dist/types/{range.d.ts → range.d.mts} +0 -0
  137. /package/dist/types/{traverse-state.d.ts → traverse-state.d.mts} +0 -0
  138. /package/dist/types/{types.d.ts → types.d.mts} +0 -0
  139. /package/dist/types/{update.d.ts → update.d.mts} +0 -0
@@ -1,872 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AsyncReducer = void 0;
4
- var tslib_1 = require("tslib");
5
- var internal_1 = require("./internal");
6
- function identity(value) {
7
- return value;
8
- }
9
- var AsyncReducer;
10
- (function (AsyncReducer) {
11
- var _this = this;
12
- /**
13
- * A base class that can be used to easily create `AsyncReducer` instances.
14
- * @typeparam I - the input value type
15
- * @typeparam O - the output value type
16
- * @typeparam S - the internal state type
17
- */
18
- var Base = /** @class */ (function () {
19
- function Base(init, next, stateToResult, onClose) {
20
- this.init = init;
21
- this.next = next;
22
- this.stateToResult = stateToResult;
23
- this.onClose = onClose;
24
- }
25
- Base.prototype.filterInput = function (pred) {
26
- var _this = this;
27
- return create(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
28
- var _a;
29
- return tslib_1.__generator(this, function (_b) {
30
- switch (_b.label) {
31
- case 0:
32
- _a = {
33
- nextIndex: 0
34
- };
35
- return [4 /*yield*/, internal_1.AsyncOptLazy.toMaybePromise(this.init)];
36
- case 1: return [2 /*return*/, (_a.state = _b.sent(),
37
- _a)];
38
- }
39
- });
40
- }); }, function (state, elem, index, halt) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
41
- var _a;
42
- return tslib_1.__generator(this, function (_b) {
43
- switch (_b.label) {
44
- case 0:
45
- if (!pred(elem, index, halt)) return [3 /*break*/, 2];
46
- _a = state;
47
- return [4 /*yield*/, this.next(state.state, elem, state.nextIndex++, halt)];
48
- case 1:
49
- _a.state = _b.sent();
50
- _b.label = 2;
51
- case 2: return [2 /*return*/, state];
52
- }
53
- });
54
- }); }, function (state) { return _this.stateToResult(state.state); }, function (state, error) { var _a; return (_a = _this.onClose) === null || _a === void 0 ? void 0 : _a.call(_this, state.state, error); });
55
- };
56
- Base.prototype.mapInput = function (mapFun) {
57
- var _this = this;
58
- return create(this.init, function (state, elem, index, halt) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _a, _b; return tslib_1.__generator(this, function (_c) {
59
- switch (_c.label) {
60
- case 0:
61
- _a = this.next;
62
- _b = [state];
63
- return [4 /*yield*/, mapFun(elem, index)];
64
- case 1: return [2 /*return*/, _a.apply(this, _b.concat([_c.sent(), index, halt]))];
65
- }
66
- }); }); }, this.stateToResult, this.onClose);
67
- };
68
- Base.prototype.collectInput = function (collectFun) {
69
- var _this = this;
70
- return create(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
71
- var _a;
72
- return tslib_1.__generator(this, function (_b) {
73
- switch (_b.label) {
74
- case 0:
75
- _a = {
76
- nextIndex: 0
77
- };
78
- return [4 /*yield*/, internal_1.AsyncOptLazy.toMaybePromise(this.init)];
79
- case 1: return [2 /*return*/, (_a.state = _b.sent(),
80
- _a)];
81
- }
82
- });
83
- }); }, function (state, elem, index, halt) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
84
- var nextElem, _a;
85
- return tslib_1.__generator(this, function (_b) {
86
- switch (_b.label) {
87
- case 0: return [4 /*yield*/, collectFun(elem, index, internal_1.CollectFun.Skip, halt)];
88
- case 1:
89
- nextElem = _b.sent();
90
- if (!(internal_1.CollectFun.Skip !== nextElem)) return [3 /*break*/, 3];
91
- _a = state;
92
- return [4 /*yield*/, this.next(state.state, nextElem, state.nextIndex++, halt)];
93
- case 2:
94
- _a.state = _b.sent();
95
- _b.label = 3;
96
- case 3: return [2 /*return*/, state];
97
- }
98
- });
99
- }); }, function (state) { return _this.stateToResult(state.state); }, function (state, error) { var _a; return (_a = _this.onClose) === null || _a === void 0 ? void 0 : _a.call(_this, state.state, error); });
100
- };
101
- Base.prototype.mapOutput = function (mapFun) {
102
- var _this = this;
103
- return create(this.init, this.next, function (state) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _a; return tslib_1.__generator(this, function (_b) {
104
- switch (_b.label) {
105
- case 0:
106
- _a = mapFun;
107
- return [4 /*yield*/, this.stateToResult(state)];
108
- case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])];
109
- }
110
- }); }); }, this.onClose);
111
- };
112
- Base.prototype.takeInput = function (amount) {
113
- if (amount <= 0) {
114
- return create(this.init, identity, this.stateToResult);
115
- }
116
- return this.filterInput(function (_, i, halt) {
117
- var more = i < amount;
118
- if (!more)
119
- halt();
120
- return more;
121
- });
122
- };
123
- Base.prototype.dropInput = function (amount) {
124
- if (amount <= 0)
125
- return this;
126
- return this.filterInput(function (_, i) { return i >= amount; });
127
- };
128
- Base.prototype.sliceInput = function (from, amount) {
129
- if (from === void 0) { from = 0; }
130
- if (undefined === amount)
131
- return this.dropInput(from);
132
- if (amount <= 0)
133
- return create(this.init, identity, this.stateToResult);
134
- if (from <= 0)
135
- return this.takeInput(amount);
136
- return this.takeInput(amount).dropInput(from);
137
- };
138
- return Base;
139
- }());
140
- AsyncReducer.Base = Base;
141
- /**
142
- * Returns an `AsyncReducer` with the given options:
143
- * @param init - the optionally lazy and/or promised initial state value
144
- * @param next - returns (potentially asynchronously) the next state value based on the given inputs:<br/>
145
- * - current: the current state<br/>
146
- * - next: the current input value<br/>
147
- * - index: the input index value<br/>
148
- * - halt: function that, when called, ensures no more elements are passed to the reducer
149
- * @param stateToResult - a potentially asynchronous function that converts the current state to an output value
150
- * @param onClose - (optional) a function that will be called when the reducer will no longer receive values
151
- * @typeparam I - the input value type
152
- * @typeparam O - the output value type
153
- * @typeparam S - the internal state type
154
- */
155
- function create(init, next, stateToResult, onClose) {
156
- return new AsyncReducer.Base(init, next, stateToResult, onClose);
157
- }
158
- AsyncReducer.create = create;
159
- /**
160
- * Returns an `AsyncReducer` of which the input, state, and output types are the same.
161
- * @param init - the optionally lazy and/or promised initial state value
162
- * @param next - returns (potentially asynchronously) the next state value based on the given inputs:<br/>
163
- * - current: the current state<br/>
164
- * - next: the current input value<br/>
165
- * - index: the input index value<br/>
166
- * - halt: function that, when called, ensures no more elements are passed to the reducer
167
- * @param stateToResult - a potentially asynchronous function that converts the current state to an output value
168
- * @param onClose - (optional) a function that will be called when the reducer will no longer receive values
169
- * @typeparam I - the input value type
170
- * @typeparam O - the output value type
171
- * @typeparam S - the internal state type
172
- */
173
- function createMono(init, next, stateToResult, onClose) {
174
- return create(init, next, stateToResult !== null && stateToResult !== void 0 ? stateToResult : identity, onClose);
175
- }
176
- AsyncReducer.createMono = createMono;
177
- /**
178
- * Returns an `AsyncReducer` of which the state and output types are the same.
179
- * @param init - the optionally lazy and/or promised initial state value
180
- * @param next - returns (potentially asynchronously) the next state value based on the given inputs:<br/>
181
- * - current: the current state<br/>
182
- * - next: the current input value<br/>
183
- * - index: the input index value<br/>
184
- * - halt: function that, when called, ensures no more elements are passed to the reducer
185
- * @param stateToResult - a potentially asynchronous function that converts the current state to an output value
186
- * @param onClose - (optional) a function that will be called when the reducer will no longer receive values
187
- * @typeparam I - the input value type
188
- * @typeparam O - the output value type
189
- * @typeparam S - the internal state type
190
- */
191
- function createOutput(init, next, stateToResult, onClose) {
192
- return create(init, next, stateToResult !== null && stateToResult !== void 0 ? stateToResult : identity, onClose);
193
- }
194
- AsyncReducer.createOutput = createOutput;
195
- function from(reducer) {
196
- return AsyncReducer.create(reducer.init, reducer.next, reducer.stateToResult);
197
- }
198
- AsyncReducer.from = from;
199
- /**
200
- * A `Reducer` that sums all given numeric input values.
201
- * @example
202
- * ```ts
203
- * console.log(Stream.range({ amount: 5 }).reduce(Reducer.sum))
204
- * // => 10
205
- * ```
206
- */
207
- AsyncReducer.sum = createMono(0, function (state, next) { return state + next; });
208
- /**
209
- * A `Reducer` that calculates the product of all given numeric input values.
210
- * @example
211
- * ```ts
212
- * console.log(Stream.range({ start: 1, amount: 5 }).reduce(product))
213
- * // => 120
214
- * ```
215
- */
216
- AsyncReducer.product = createMono(1, function (state, next, _, halt) {
217
- if (0 === next)
218
- halt();
219
- return state * next;
220
- });
221
- /**
222
- * A `Reducer` that calculates the average of all given numberic input values.
223
- * @example
224
- * ```ts
225
- * console.log(Stream.range({ amount: 5 }).reduce(Reducer.average));
226
- * // => 2
227
- * ```
228
- */
229
- AsyncReducer.average = createMono(0, function (avg, value, index) { return avg + (value - avg) / (index + 1); });
230
- /**
231
- * Returns a `Reducer` that remembers the minimum value of the inputs using the given `compFun` to compare input values
232
- * @param compFun - a comparison function for two input values, returning 0 when equal, positive when greater, negetive when smaller
233
- * @param otherwise - (default: undefineds) a fallback value when there were no input values given
234
- * @example
235
- * ```ts
236
- * const stream = Stream.of('abc', 'a', 'abcde', 'ab')
237
- * console.log(stream.minBy((s1, s2) => s1.length - s2.length))
238
- * // 'a'
239
- * ```
240
- */
241
- AsyncReducer.minBy = function (compFun, otherwise) {
242
- var token = Symbol();
243
- return create(token, function (state, next) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
244
- return tslib_1.__generator(this, function (_a) {
245
- switch (_a.label) {
246
- case 0:
247
- if (token === state)
248
- return [2 /*return*/, next];
249
- return [4 /*yield*/, compFun(state, next)];
250
- case 1: return [2 /*return*/, (_a.sent()) < 0 ? state : next];
251
- }
252
- });
253
- }); }, function (state) {
254
- return token === state ? internal_1.AsyncOptLazy.toMaybePromise(otherwise) : state;
255
- });
256
- };
257
- /**
258
- * Returns a `Reducer` that remembers the minimum value of the numberic inputs.
259
- * @param otherwise - (default: undefined) a fallback value when there were no input values given
260
- * @example
261
- * ```ts
262
- * console.log(Stream.of(5, 3, 7, 4).reduce(Reducer.min()))
263
- * // => 3
264
- * ```
265
- */
266
- AsyncReducer.min = function (otherwise) {
267
- return create(undefined, function (state, next) {
268
- return undefined !== state && state < next ? state : next;
269
- }, function (state) {
270
- return state !== null && state !== void 0 ? state : internal_1.AsyncOptLazy.toMaybePromise(otherwise);
271
- });
272
- };
273
- /**
274
- * Returns a `Reducer` that remembers the maximum value of the inputs using the given `compFun` to compare input values
275
- * @param compFun - a comparison function for two input values, returning 0 when equal, positive when greater, negetive when smaller
276
- * @param otherwise - (default: undefined) a fallback value when there were no input values given
277
- * @example
278
- * ```ts
279
- * const stream = Stream.of('abc', 'a', 'abcde', 'ab')
280
- * console.log(stream.maxBy((s1, s2) => s1.length - s2.length))
281
- * // 'abcde'
282
- * ```
283
- */
284
- AsyncReducer.maxBy = function (compFun, otherwise) {
285
- var token = Symbol();
286
- return create(token, function (state, next) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
287
- return tslib_1.__generator(this, function (_a) {
288
- switch (_a.label) {
289
- case 0:
290
- if (token === state)
291
- return [2 /*return*/, next];
292
- return [4 /*yield*/, compFun(state, next)];
293
- case 1: return [2 /*return*/, (_a.sent()) > 0 ? state : next];
294
- }
295
- });
296
- }); }, function (state) {
297
- return token === state ? internal_1.AsyncOptLazy.toMaybePromise(otherwise) : state;
298
- });
299
- };
300
- /**
301
- * Returns a `Reducer` that remembers the maximum value of the numberic inputs.
302
- * @param otherwise - (default: undefined) a fallback value when there were no input values given
303
- * @example
304
- * ```ts
305
- * console.log(Stream.of(5, 3, 7, 4).reduce(Reducer.max()))
306
- * // => 7
307
- * ```
308
- */
309
- AsyncReducer.max = function (otherwise) {
310
- return create(undefined, function (state, next) {
311
- return undefined !== state && state > next ? state : next;
312
- }, function (state) {
313
- return state !== null && state !== void 0 ? state : internal_1.AsyncOptLazy.toMaybePromise(otherwise);
314
- });
315
- };
316
- /**
317
- * Returns a `Reducer` that joins the given input values into a string using the given options.
318
- * @param options - an object containing:<br/>
319
- * - sep: (optional) a seperator string value between values in the output<br/>
320
- * - start: (optional) a start string to prepend to the output<br/>
321
- * - end: (optional) an end string to append to the output<br/>
322
- * @example
323
- * ```ts
324
- * console.log(Stream.of(1, 2, 3).reduce(Reducer.join({ sep: '-' })))
325
- * // => '1-2-3'
326
- * ```
327
- */
328
- function join(_a) {
329
- var _b = _a === void 0 ? {} : _a, _c = _b.sep, sep = _c === void 0 ? '' : _c, _d = _b.start, start = _d === void 0 ? '' : _d, _e = _b.end, end = _e === void 0 ? '' : _e, _f = _b.valueToString, valueToString = _f === void 0 ? String : _f;
330
- var curSep = '';
331
- var curStart = start;
332
- return create('', function (state, next) {
333
- var result = curStart.concat(state, curSep, valueToString(next));
334
- curSep = sep;
335
- curStart = '';
336
- return result;
337
- }, function (state) { return state.concat(end); });
338
- }
339
- AsyncReducer.join = join;
340
- /**
341
- * Returns an `AsyncReducer` that remembers the amount of input items provided.
342
- * @param pred - (optional) a predicate that returns false if the item should not be counted given:<br/>
343
- * - value: the current input value<br/>
344
- * - index: the input value index
345
- * @example
346
- * ```ts
347
- * const stream = AsyncStream.from(Stream.range({ amount: 10 }))
348
- * console.log(await stream.reduce(AsyncReducer.count()))
349
- * // => 10
350
- * console.log(await stream.reduce(AsyncReducer.count(async v => v < 5)))
351
- * // => 5
352
- * ```
353
- */
354
- AsyncReducer.count = function (pred) {
355
- if (undefined === pred)
356
- return createOutput(0, function (_, __, i) { return i + 1; });
357
- return createOutput(0, function (state, next, i) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
358
- return tslib_1.__generator(this, function (_a) {
359
- switch (_a.label) {
360
- case 0: return [4 /*yield*/, (pred === null || pred === void 0 ? void 0 : pred(next, i))];
361
- case 1:
362
- if (_a.sent())
363
- return [2 /*return*/, state + 1];
364
- return [2 /*return*/, state];
365
- }
366
- });
367
- }); });
368
- };
369
- /**
370
- * Returns an `AsyncReducer` that remembers the first input value for which the given `pred` function returns true.
371
- * @param pred - a function taking an input value and its index, and returning true if the value should be remembered
372
- * @param otherwise - (default: undefined) a fallback value to output if no input value yet has satisfied the given predicate
373
- * @typeparam T - the input value type
374
- * @typeparam O - the fallback value type
375
- * @example
376
- * ```ts
377
- * await AsyncStream.from(Stream.range({ amount: 10 })).reduce(AsyncReducer.firstWhere(async v => v > 5)))
378
- * // => 6
379
- * ```
380
- */
381
- AsyncReducer.firstWhere = function (pred, otherwise) {
382
- var token = Symbol();
383
- return create(token, function (state, next, i, halt) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
384
- var _a;
385
- return tslib_1.__generator(this, function (_b) {
386
- switch (_b.label) {
387
- case 0:
388
- _a = token === state;
389
- if (!_a) return [3 /*break*/, 2];
390
- return [4 /*yield*/, pred(next, i)];
391
- case 1:
392
- _a = (_b.sent());
393
- _b.label = 2;
394
- case 2:
395
- if (_a) {
396
- halt();
397
- return [2 /*return*/, next];
398
- }
399
- return [2 /*return*/, state];
400
- }
401
- });
402
- }); }, function (state) {
403
- return token === state ? internal_1.AsyncOptLazy.toMaybePromise(otherwise) : state;
404
- });
405
- };
406
- /**
407
- * Returns an `AsyncReducer` that remembers the first input value.
408
- * @param otherwise - (default: undefined) a fallback value to output if no input value has been provided
409
- * @typeparam T - the input value type
410
- * @typeparam O - the fallback value type
411
- * @example
412
- * ```ts
413
- * await AsyncStream.from(Stream.range{ amount: 10 })).reduce(AsyncReducer.first())
414
- * // => 0
415
- * ```
416
- */
417
- AsyncReducer.first = function (otherwise) {
418
- var token = Symbol();
419
- return create(token, function (state, next, _, halt) {
420
- halt();
421
- if (token === state)
422
- return next;
423
- return state;
424
- }, function (state) {
425
- return token === state ? internal_1.AsyncOptLazy.toMaybePromise(otherwise) : state;
426
- });
427
- };
428
- /**
429
- * Returns an `AsyncReducer` that remembers the last input value for which the given `pred` function returns true.
430
- * @param pred - a function taking an input value and its index, and returning true if the value should be remembered
431
- * @param otherwise - (default: undefined) a fallback value to output if no input value yet has satisfied the given predicate
432
- * @typeparam T - the input value type
433
- * @typeparam O - the fallback value type
434
- * @example
435
- * ```ts
436
- * await AsyncStream.from(Stream.range({ amount: 10 })).reduce(AsyncReducer.lastWhere(async v => v > 5)))
437
- * // => 9
438
- * ```
439
- */
440
- AsyncReducer.lastWhere = function (pred, otherwise) {
441
- var token = Symbol();
442
- return create(token, function (state, next, i) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
443
- return tslib_1.__generator(this, function (_a) {
444
- switch (_a.label) {
445
- case 0: return [4 /*yield*/, pred(next, i)];
446
- case 1:
447
- if (_a.sent())
448
- return [2 /*return*/, next];
449
- return [2 /*return*/, state];
450
- }
451
- });
452
- }); }, function (state) {
453
- return token === state ? internal_1.AsyncOptLazy.toMaybePromise(otherwise) : state;
454
- });
455
- };
456
- /**
457
- * Returns an `AsyncReducer` that remembers the last input value.
458
- * @param otherwise - (default: undefined) a fallback value to output if no input value has been provided
459
- * @typeparam T - the input value type
460
- * @typeparam O - the fallback value type
461
- * @example
462
- * ```ts
463
- * await AsyncStream.from(Stream.range{ amount: 10 })).reduce(AsyncReducer.last())
464
- * // => 9
465
- * ```
466
- */
467
- AsyncReducer.last = function (otherwise) {
468
- var token = Symbol();
469
- return create(function () { return token; }, function (_, next) { return next; }, function (state) {
470
- return token === state ? internal_1.AsyncOptLazy.toMaybePromise(otherwise) : state;
471
- });
472
- };
473
- /**
474
- * Returns a `Reducer` that ouputs false as long as no input value satisfies given `pred`, true otherwise.
475
- * @typeparam T - the element type
476
- * @param pred - a function taking an input value and its index, and returning true if the value satisfies the predicate
477
- * @example
478
- * ```ts
479
- * await AsyncStream.from(Stream.range{ amount: 10 })).reduce(AsyncReducer.some(async v => v > 5))
480
- * // => true
481
- * ```
482
- */
483
- function some(pred) {
484
- var _this = this;
485
- return createOutput(false, function (state, next, i, halt) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
486
- var satisfies;
487
- return tslib_1.__generator(this, function (_a) {
488
- switch (_a.label) {
489
- case 0:
490
- if (state)
491
- return [2 /*return*/, state];
492
- return [4 /*yield*/, pred(next, i)];
493
- case 1:
494
- satisfies = _a.sent();
495
- if (satisfies) {
496
- halt();
497
- }
498
- return [2 /*return*/, satisfies];
499
- }
500
- });
501
- }); });
502
- }
503
- AsyncReducer.some = some;
504
- /**
505
- * Returns an `AsyncReducer` that ouputs true as long as all input values satisfy the given `pred`, false otherwise.
506
- * @typeparam T - the element type
507
- * @param pred - a function taking an input value and its index, and returning true if the value satisfies the predicate
508
- * @example
509
- * ```ts
510
- * await AsyncStream.from(Stream.range{ amount: 10 })).reduce(AsyncReducer.every(async v => v < 5))
511
- * // => false
512
- * ```
513
- */
514
- function every(pred) {
515
- var _this = this;
516
- return createOutput(true, function (state, next, i, halt) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
517
- var satisfies;
518
- return tslib_1.__generator(this, function (_a) {
519
- switch (_a.label) {
520
- case 0:
521
- if (!state)
522
- return [2 /*return*/, state];
523
- return [4 /*yield*/, pred(next, i)];
524
- case 1:
525
- satisfies = _a.sent();
526
- if (!satisfies) {
527
- halt();
528
- }
529
- return [2 /*return*/, satisfies];
530
- }
531
- });
532
- }); });
533
- }
534
- AsyncReducer.every = every;
535
- /**
536
- * Returns an `AsyncReducer` that outputs false as long as the given `elem` has not been encountered in the input values, true otherwise.
537
- * @typeparam T - the element type
538
- * @param elem - the element to search for
539
- * @param eq - (optional) a comparison function that returns true if te two given input values are considered equal
540
- * @example
541
- * ```ts
542
- * await AsyncStream.from(Stream.range({ amount: 10 })).reduce(AsyncReducer.contains(5)))
543
- * // => true
544
- * ```
545
- */
546
- function contains(elem, eq) {
547
- if (eq === void 0) { eq = Object.is; }
548
- return createOutput(false, function (state, next, _, halt) {
549
- if (state)
550
- return state;
551
- var satisfies = eq(next, elem);
552
- if (satisfies) {
553
- halt();
554
- }
555
- return satisfies;
556
- });
557
- }
558
- AsyncReducer.contains = contains;
559
- /**
560
- * Returns an `AsyncReducer` that takes boolean values and outputs true if all input values are true, and false otherwise.
561
- * @example
562
- * ```ts
563
- * await AsyncStream.of(true, false, true)).reduce(AsyncReducer.and))
564
- * // => false
565
- * ```
566
- */
567
- AsyncReducer.and = createMono(true, function (state, next, _, halt) {
568
- if (!state)
569
- return state;
570
- if (!next)
571
- halt();
572
- return next;
573
- });
574
- /**
575
- * Returns an `AsyncReducer` that takes boolean values and outputs true if one or more input values are true, and false otherwise.
576
- * @example
577
- * ```ts
578
- * await AsyncStream.of(true, false, true)).reduce(AsyncReducer.or))
579
- * // => true
580
- * ```
581
- */
582
- AsyncReducer.or = createMono(false, function (state, next, _, halt) {
583
- if (state)
584
- return state;
585
- if (next)
586
- halt();
587
- return next;
588
- });
589
- /**
590
- * Returns an `AsyncReducer` that outputs true if no input values are received, false otherwise.
591
- * @example
592
- * ```ts
593
- * await AsyncStream.of(1, 2, 3).reduce(AsyncReducer.isEmpty))
594
- * // => false
595
- * ```
596
- */
597
- AsyncReducer.isEmpty = createOutput(true, function (_, __, ___, halt) {
598
- halt();
599
- return false;
600
- });
601
- /**
602
- * Returns an `AsyncReducer` that outputs true if one or more input values are received, false otherwise.
603
- * @example
604
- * ```ts
605
- * await AsyncStream.of(1, 2, 3).reduce(AsyncReducer.nonEmpty))
606
- * // => true
607
- * ```
608
- */
609
- AsyncReducer.nonEmpty = createOutput(false, function (_, __, ___, halt) {
610
- halt();
611
- return true;
612
- });
613
- /**
614
- * Returns an `AsyncReducer` that collects received input values in an array, and returns a copy of that array as an output value when requested.
615
- * @typeparam T - the element type
616
- * @example
617
- * ```ts
618
- * await AsyncStream.of(1, 2, 3).reduce(AsyncReducer.toArray()))
619
- * // => [1, 2, 3]
620
- * ```
621
- */
622
- function toArray() {
623
- return create(function () { return []; }, function (state, next) {
624
- state.push(next);
625
- return state;
626
- }, function (state) { return state.slice(); });
627
- }
628
- AsyncReducer.toArray = toArray;
629
- /**
630
- * Returns a `AsyncReducer` that collects received input tuples into a mutable JS Map, and returns
631
- * a copy of that map when output is requested.
632
- * @typeparam K - the map key type
633
- * @typeparam V - the map value type
634
- * @example
635
- * ```ts
636
- * await AsyncStream.of([1, 'a'], [2, 'b']).reduce(AsyncReducer.toJSMap()))
637
- * // Map { 1 => 'a', 2 => 'b' }
638
- * ```
639
- */
640
- function toJSMap() {
641
- return create(function () { return new Map(); }, function (state, next) {
642
- state.set(next[0], next[1]);
643
- return state;
644
- }, function (s) { return new Map(s); });
645
- }
646
- AsyncReducer.toJSMap = toJSMap;
647
- /**
648
- * Returns an `AsyncReducer` that collects received input values into a mutable JS Set, and returns
649
- * a copy of that map when output is requested.
650
- * @typeparam T - the element type
651
- * @example
652
- * ```ts
653
- * await AsyncStream.of(1, 2, 3).reduce(AsyncReducer.toJSSet()))
654
- * // Set {1, 2, 3}
655
- * ```
656
- */
657
- function toJSSet() {
658
- return create(function () { return new Set(); }, function (state, next) {
659
- state.add(next);
660
- return state;
661
- }, function (s) { return new Set(s); });
662
- }
663
- AsyncReducer.toJSSet = toJSSet;
664
- /**
665
- * Returns an `AsyncReducer` that collects 2-tuples containing keys and values into a plain JS object, and
666
- * returns a copy of that object when output is requested.
667
- * @typeparam K - the result object key type
668
- * @typeparam V - the result object value type
669
- * @example
670
- * ```ts
671
- * await AsyncStream.of(['a', 1], ['b', true]).reduce(AsyncReducer.toJSObject()))
672
- * // { a: 1, b: true }
673
- * ```
674
- */
675
- function toJSObject() {
676
- return create(function () { return ({}); }, function (state, entry) {
677
- state[entry[0]] = entry[1];
678
- return state;
679
- }, function (s) { return (tslib_1.__assign({}, s)); });
680
- }
681
- AsyncReducer.toJSObject = toJSObject;
682
- /**
683
- * Returns an `AsyncReducer` that combines multiple input `reducers` by providing input values to all of them and collecting the outputs in an array.
684
- * @param reducers - 2 or more reducers to combine
685
- * @example
686
- * ```ts
687
- * const red = AsyncReducer.combineArr(AsyncReducer.sum, AsyncReducer.average)
688
- *
689
- * await AsyncStream.from(Stream.range({ amount: 9 }))
690
- * .reduce(red)
691
- * // => [36, 4]
692
- * ```
693
- */
694
- function combineArr() {
695
- var _this = this;
696
- var reducers = [];
697
- for (var _i = 0; _i < arguments.length; _i++) {
698
- reducers[_i] = arguments[_i];
699
- }
700
- var createState = function () {
701
- return Promise.all(reducers.map(function (reducer) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
702
- var result;
703
- var _a;
704
- return tslib_1.__generator(this, function (_b) {
705
- switch (_b.label) {
706
- case 0:
707
- _a = {
708
- reducer: reducer,
709
- halted: false,
710
- halt: function () {
711
- result.halted = true;
712
- }
713
- };
714
- return [4 /*yield*/, internal_1.AsyncOptLazy.toMaybePromise(reducer.init)];
715
- case 1:
716
- result = (_a.state = _b.sent(),
717
- _a);
718
- return [2 /*return*/, result];
719
- }
720
- });
721
- }); }));
722
- };
723
- return create(createState, function (allState, next, index, halt) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
724
- var anyNotHalted;
725
- var _this = this;
726
- return tslib_1.__generator(this, function (_a) {
727
- switch (_a.label) {
728
- case 0:
729
- anyNotHalted = false;
730
- return [4 /*yield*/, Promise.all(allState.map(function (red) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
731
- var _a;
732
- return tslib_1.__generator(this, function (_b) {
733
- switch (_b.label) {
734
- case 0:
735
- if (red.halted)
736
- return [2 /*return*/];
737
- _a = red;
738
- return [4 /*yield*/, red.reducer.next(red.state, next, index, red.halt)];
739
- case 1:
740
- _a.state = _b.sent();
741
- if (!red.halted)
742
- anyNotHalted = true;
743
- return [2 /*return*/];
744
- }
745
- });
746
- }); }))];
747
- case 1:
748
- _a.sent();
749
- if (!anyNotHalted)
750
- halt();
751
- return [2 /*return*/, allState];
752
- }
753
- });
754
- }); }, function (allState) {
755
- return Promise.all(allState.map(function (st) { return st.reducer.stateToResult(st.state); }));
756
- });
757
- }
758
- AsyncReducer.combineArr = combineArr;
759
- /**
760
- * Returns an `AsyncReducer` that combines multiple input `reducers` by providing input values to all of them and collecting the outputs in the shape of the given object.
761
- * @typeparam T - the input type for all the reducers
762
- * @typeparam R - the result object shape
763
- * @param reducerObj - an object of keys, and reducers corresponding to those keys
764
- * @example
765
- * ```ts
766
- * const red = AsyncReducer.combineObj({
767
- * theSum: Reducer.sum,
768
- * theAverage: Reducer.average
769
- * });
770
- *
771
- * await AsyncStream.from(Stream.range({ amount: 9 }))
772
- * .reduce(red));
773
- * // => { theSum: 36, theAverage: 4 }
774
- * ```
775
- */
776
- function combineObj(reducerObj) {
777
- var _this = this;
778
- var createState = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
779
- var entries;
780
- var _this = this;
781
- return tslib_1.__generator(this, function (_a) {
782
- switch (_a.label) {
783
- case 0: return [4 /*yield*/, Promise.all(Object.entries(reducerObj).map(function (_a) {
784
- var _b = tslib_1.__read(_a, 2), key = _b[0], reducer = _b[1];
785
- return tslib_1.__awaiter(_this, void 0, void 0, function () {
786
- var result;
787
- var _c;
788
- return tslib_1.__generator(this, function (_d) {
789
- switch (_d.label) {
790
- case 0:
791
- _c = {
792
- reducer: reducer,
793
- halted: false,
794
- halt: function () {
795
- result.halted = true;
796
- }
797
- };
798
- return [4 /*yield*/, internal_1.AsyncOptLazy.toMaybePromise(reducer.init)];
799
- case 1:
800
- result = (_c.state = _d.sent(),
801
- _c);
802
- return [2 /*return*/, [key, result]];
803
- }
804
- });
805
- });
806
- }))];
807
- case 1:
808
- entries = _a.sent();
809
- return [2 /*return*/, Object.fromEntries(entries)];
810
- }
811
- });
812
- }); };
813
- return create(createState, function (allState, next, index, halt) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
814
- var anyNotHalted;
815
- var _this = this;
816
- return tslib_1.__generator(this, function (_a) {
817
- switch (_a.label) {
818
- case 0:
819
- anyNotHalted = false;
820
- return [4 /*yield*/, Promise.all(Object.values(allState).map(function (red) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
821
- var _a;
822
- return tslib_1.__generator(this, function (_b) {
823
- switch (_b.label) {
824
- case 0:
825
- if (red.halted) {
826
- return [2 /*return*/];
827
- }
828
- _a = red;
829
- return [4 /*yield*/, red.reducer.next(red.state, next, index, red.halt)];
830
- case 1:
831
- _a.state = _b.sent();
832
- if (!red.halted) {
833
- anyNotHalted = true;
834
- }
835
- return [2 /*return*/];
836
- }
837
- });
838
- }); }))];
839
- case 1:
840
- _a.sent();
841
- if (!anyNotHalted) {
842
- halt();
843
- }
844
- return [2 /*return*/, allState];
845
- }
846
- });
847
- }); }, function (allState) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
848
- var entries;
849
- var _this = this;
850
- return tslib_1.__generator(this, function (_a) {
851
- switch (_a.label) {
852
- case 0: return [4 /*yield*/, Promise.all(Object.entries(allState).map(function (_a) {
853
- var _b = tslib_1.__read(_a, 2), key = _b[0], st = _b[1];
854
- return tslib_1.__awaiter(_this, void 0, void 0, function () { var _c; return tslib_1.__generator(this, function (_d) {
855
- switch (_d.label) {
856
- case 0:
857
- _c = [key];
858
- return [4 /*yield*/, st.reducer.stateToResult(st.state)];
859
- case 1: return [2 /*return*/, _c.concat([_d.sent()])];
860
- }
861
- }); });
862
- }))];
863
- case 1:
864
- entries = _a.sent();
865
- return [2 /*return*/, Object.fromEntries(entries)];
866
- }
867
- });
868
- }); });
869
- }
870
- AsyncReducer.combineObj = combineObj;
871
- })(AsyncReducer = exports.AsyncReducer || (exports.AsyncReducer = {}));
872
- //# sourceMappingURL=async-reducer.js.map