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