@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
@@ -0,0 +1,1685 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.mts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ AsyncOptLazy: () => AsyncOptLazy,
24
+ AsyncReducer: () => AsyncReducer,
25
+ CollectFun: () => CollectFun,
26
+ Comp: () => Comp,
27
+ Eq: () => Eq,
28
+ Err: () => Err,
29
+ ErrBase: () => ErrBase,
30
+ IndexRange: () => IndexRange,
31
+ OptLazy: () => OptLazy,
32
+ OptLazyOr: () => OptLazyOr,
33
+ Range: () => Range,
34
+ Reducer: () => Reducer,
35
+ TraverseState: () => TraverseState,
36
+ Update: () => Update
37
+ });
38
+ module.exports = __toCommonJS(src_exports);
39
+
40
+ // src/collect.mts
41
+ var CollectFun;
42
+ ((CollectFun2) => {
43
+ CollectFun2.Skip = Symbol("Skip");
44
+ })(CollectFun || (CollectFun = {}));
45
+
46
+ // src/comp.mts
47
+ var Comp;
48
+ ((Comp2) => {
49
+ const _anyFlatComp = createAnyComp("FLAT");
50
+ const _anyShallowComp = createAnyComp("SHALLOW");
51
+ const _anyDeepComp = createAnyComp("DEEP");
52
+ function defaultComp() {
53
+ return _anyDeepComp;
54
+ }
55
+ Comp2.defaultComp = defaultComp;
56
+ const _numberComp = {
57
+ isComparable(obj) {
58
+ return typeof obj === "number";
59
+ },
60
+ compare(v1, v2) {
61
+ if (Number.isFinite(v1) && Number.isFinite(v2)) {
62
+ return v1 - v2;
63
+ }
64
+ if (Number.isNaN(v1)) {
65
+ if (Number.isNaN(v2))
66
+ return 0;
67
+ if (v2 === Number.POSITIVE_INFINITY)
68
+ return 1;
69
+ if (v2 === Number.NEGATIVE_INFINITY)
70
+ return -1;
71
+ return -1;
72
+ }
73
+ if (v1 === Number.POSITIVE_INFINITY) {
74
+ return v2 === Number.POSITIVE_INFINITY ? 0 : 1;
75
+ }
76
+ return v2 === Number.NEGATIVE_INFINITY ? 0 : -1;
77
+ }
78
+ };
79
+ function numberComp() {
80
+ return _numberComp;
81
+ }
82
+ Comp2.numberComp = numberComp;
83
+ const _booleanComp = {
84
+ isComparable(obj) {
85
+ return typeof obj === "boolean";
86
+ },
87
+ compare(v1, v2) {
88
+ return v1 === v2 ? 0 : v1 ? 1 : -1;
89
+ }
90
+ };
91
+ function booleanComp() {
92
+ return _booleanComp;
93
+ }
94
+ Comp2.booleanComp = booleanComp;
95
+ const _bigIntComp = {
96
+ isComparable(obj) {
97
+ return typeof obj === "bigint";
98
+ },
99
+ compare(v1, v2) {
100
+ const res = v1 - v2;
101
+ if (res > 0)
102
+ return 1;
103
+ if (res < 0)
104
+ return -1;
105
+ return 0;
106
+ }
107
+ };
108
+ function bigIntComp() {
109
+ return _bigIntComp;
110
+ }
111
+ Comp2.bigIntComp = bigIntComp;
112
+ const _defaultCollator = Intl.Collator("und");
113
+ const _stringComp = {
114
+ isComparable(obj) {
115
+ return typeof obj === "string";
116
+ },
117
+ compare: _defaultCollator.compare
118
+ };
119
+ const _anyStringJSONComp = {
120
+ isComparable(obj) {
121
+ return true;
122
+ },
123
+ compare(v1, v2) {
124
+ return _defaultCollator.compare(JSON.stringify(v1), JSON.stringify(v2));
125
+ }
126
+ };
127
+ function anyStringJSONComp() {
128
+ return _anyStringJSONComp;
129
+ }
130
+ Comp2.anyStringJSONComp = anyStringJSONComp;
131
+ function stringComp(...args) {
132
+ if (args.length === 0)
133
+ return _stringComp;
134
+ const collator = Intl.Collator(...args);
135
+ return {
136
+ isComparable(obj) {
137
+ return typeof obj === "string";
138
+ },
139
+ compare: collator.compare
140
+ };
141
+ }
142
+ Comp2.stringComp = stringComp;
143
+ function stringCaseInsensitiveComp() {
144
+ return stringComp("und", { sensitivity: "accent" });
145
+ }
146
+ Comp2.stringCaseInsensitiveComp = stringCaseInsensitiveComp;
147
+ const _stringCharCodeComp = {
148
+ isComparable(obj) {
149
+ return typeof obj === "string";
150
+ },
151
+ compare(v1, v2) {
152
+ const len = Math.min(v1.length, v2.length);
153
+ let i = -1;
154
+ while (++i < len) {
155
+ const diff = v1.charCodeAt(i) - v2.charCodeAt(i);
156
+ if (diff !== 0)
157
+ return diff;
158
+ }
159
+ return v1.length - v2.length;
160
+ }
161
+ };
162
+ function stringCharCodeComp() {
163
+ return _stringCharCodeComp;
164
+ }
165
+ Comp2.stringCharCodeComp = stringCharCodeComp;
166
+ const _anyToStringComp = {
167
+ isComparable(obj) {
168
+ return true;
169
+ },
170
+ compare(v1, v2) {
171
+ return _defaultCollator.compare(
172
+ Eq.convertAnyToString(v1),
173
+ Eq.convertAnyToString(v2)
174
+ );
175
+ }
176
+ };
177
+ function anyToStringComp() {
178
+ return _anyToStringComp;
179
+ }
180
+ Comp2.anyToStringComp = anyToStringComp;
181
+ function createValueOfComp(cls, valueComp = anyShallowComp()) {
182
+ return {
183
+ isComparable(obj) {
184
+ return obj instanceof cls;
185
+ },
186
+ compare(v1, v2) {
187
+ return valueComp.compare(v1.valueOf(), v2.valueOf());
188
+ }
189
+ };
190
+ }
191
+ Comp2.createValueOfComp = createValueOfComp;
192
+ const _DateComp = createValueOfComp(Date, _numberComp);
193
+ function dateComp() {
194
+ return _DateComp;
195
+ }
196
+ Comp2.dateComp = dateComp;
197
+ function createIterableComp(itemComp) {
198
+ return {
199
+ isComparable(obj) {
200
+ return typeof obj === "object" && obj !== null && Symbol.iterator in obj;
201
+ },
202
+ compare(v1, v2) {
203
+ const iter1 = v1[Symbol.iterator]();
204
+ const iter2 = v2[Symbol.iterator]();
205
+ while (true) {
206
+ const value1 = iter1.next();
207
+ const value2 = iter2.next();
208
+ if (value1.done)
209
+ return value2.done ? 0 : -1;
210
+ if (value2.done)
211
+ return 1;
212
+ const result = itemComp.compare(value1.value, value2.value);
213
+ if (result !== 0)
214
+ return result;
215
+ }
216
+ }
217
+ };
218
+ }
219
+ const _iterableAnyComp = createIterableComp(
220
+ Comp2.defaultComp()
221
+ );
222
+ function iterableComp(itemComp) {
223
+ if (void 0 === itemComp)
224
+ return _iterableAnyComp;
225
+ return createIterableComp(itemComp);
226
+ }
227
+ Comp2.iterableComp = iterableComp;
228
+ const _BooleanComp = createValueOfComp(Boolean, _booleanComp);
229
+ const _NumberComp = createValueOfComp(Number, _numberComp);
230
+ const _StringComp = createValueOfComp(String, _stringComp);
231
+ const _wrappedComps = [
232
+ _BooleanComp,
233
+ _DateComp,
234
+ _NumberComp,
235
+ _StringComp
236
+ ];
237
+ function tryWrappedComp(v1, v2) {
238
+ let i = -1;
239
+ const len = _wrappedComps.length;
240
+ while (++i < len) {
241
+ const comp = _wrappedComps[i];
242
+ if (comp.isComparable(v1) && comp.isComparable(v2)) {
243
+ return comp.compare(v1, v2);
244
+ }
245
+ }
246
+ return void 0;
247
+ }
248
+ function createObjectComp(keyComp = anyFlatComp(), valueComp = defaultComp()) {
249
+ return {
250
+ isComparable(obj) {
251
+ return true;
252
+ },
253
+ compare(v1, v2) {
254
+ const keys1 = Object.keys(v1);
255
+ const keys2 = Object.keys(v2);
256
+ if (keys1.length === 0) {
257
+ return keys2.length === 0 ? 0 : -1;
258
+ }
259
+ if (keys2.length === 0) {
260
+ return keys1.length === 0 ? 0 : 1;
261
+ }
262
+ keys1.sort(keyComp.compare);
263
+ keys2.sort(keyComp.compare);
264
+ const length = Math.min(keys1.length, keys2.length);
265
+ for (let index = 0; index < length; index++) {
266
+ const key1 = keys1[index];
267
+ const key2 = keys2[index];
268
+ const keyResult = keyComp.compare(key1, key2);
269
+ if (keyResult !== 0)
270
+ return keyResult;
271
+ const value1 = v1[key1];
272
+ const value2 = v2[key2];
273
+ const valueResult = valueComp.compare(value1, value2);
274
+ if (valueResult !== 0)
275
+ return valueResult;
276
+ }
277
+ const keyDiff = keys1.length - keys2.length;
278
+ return keyDiff;
279
+ }
280
+ };
281
+ }
282
+ const _objectAnyComp = createObjectComp();
283
+ function objectComp(options) {
284
+ if (void 0 === options)
285
+ return _objectAnyComp;
286
+ return createObjectComp(options.keyComp, options.valueComp);
287
+ }
288
+ Comp2.objectComp = objectComp;
289
+ function createAnyComp(mode) {
290
+ return {
291
+ isComparable(obj) {
292
+ return true;
293
+ },
294
+ compare(v1, v2) {
295
+ if (Object.is(v1, v2))
296
+ return 0;
297
+ const type1 = typeof v1;
298
+ const type2 = typeof v2;
299
+ if (type1 !== type2) {
300
+ return _anyToStringComp.compare(v1, v2);
301
+ }
302
+ switch (type1) {
303
+ case "bigint":
304
+ return _bigIntComp.compare(v1, v2);
305
+ case "boolean":
306
+ return _booleanComp.compare(v1, v2);
307
+ case "number":
308
+ return _numberComp.compare(v1, v2);
309
+ case "string":
310
+ return _stringComp.compare(v1, v2);
311
+ case "object": {
312
+ if (null === v1) {
313
+ if (null === v2)
314
+ return 0;
315
+ return -1;
316
+ }
317
+ if (null === v2) {
318
+ return 1;
319
+ }
320
+ const wrappedComp = tryWrappedComp(v1, v2);
321
+ if (void 0 !== wrappedComp)
322
+ return wrappedComp;
323
+ if (mode !== "FLAT") {
324
+ if (_iterableAnyComp.isComparable(v1) && _iterableAnyComp.isComparable(v2)) {
325
+ if (mode === "SHALLOW") {
326
+ return iterableComp(_anyFlatComp).compare(v1, v2);
327
+ }
328
+ return iterableComp(this).compare(v1, v2);
329
+ }
330
+ if (mode === "SHALLOW") {
331
+ return createObjectComp(_anyFlatComp, _anyFlatComp).compare(
332
+ v1,
333
+ v2
334
+ );
335
+ }
336
+ return _objectAnyComp.compare(v1, v2);
337
+ }
338
+ }
339
+ }
340
+ return _anyToStringComp.compare(v1, v2);
341
+ }
342
+ };
343
+ }
344
+ function anyFlatComp() {
345
+ return _anyFlatComp;
346
+ }
347
+ Comp2.anyFlatComp = anyFlatComp;
348
+ function anyShallowComp() {
349
+ return _anyShallowComp;
350
+ }
351
+ Comp2.anyShallowComp = anyShallowComp;
352
+ function anyDeepComp() {
353
+ return _anyDeepComp;
354
+ }
355
+ Comp2.anyDeepComp = anyDeepComp;
356
+ function withUndefined(comp) {
357
+ return {
358
+ isComparable(obj) {
359
+ return void 0 === obj || comp.isComparable(obj);
360
+ },
361
+ compare(v1, v2) {
362
+ if (void 0 === v1) {
363
+ if (void 0 === v2)
364
+ return 0;
365
+ return -1;
366
+ }
367
+ if (void 0 === v2)
368
+ return 1;
369
+ return comp.compare(v1, v2);
370
+ }
371
+ };
372
+ }
373
+ Comp2.withUndefined = withUndefined;
374
+ function withNull(comp) {
375
+ return {
376
+ isComparable(obj) {
377
+ return null === obj || comp.isComparable(obj);
378
+ },
379
+ compare(v1, v2) {
380
+ if (null === v1) {
381
+ if (null === v2)
382
+ return 0;
383
+ return -1;
384
+ }
385
+ if (null === v2)
386
+ return 1;
387
+ return comp.compare(v1, v2);
388
+ }
389
+ };
390
+ }
391
+ Comp2.withNull = withNull;
392
+ function invert(comp) {
393
+ return {
394
+ compare(v1, v2) {
395
+ return comp.compare(v2, v1);
396
+ },
397
+ isComparable: comp.isComparable
398
+ };
399
+ }
400
+ Comp2.invert = invert;
401
+ function toEq(comp) {
402
+ return (v1, v2) => comp.compare(v1, v2) === 0;
403
+ }
404
+ Comp2.toEq = toEq;
405
+ })(Comp || (Comp = {}));
406
+
407
+ // src/eq.mts
408
+ var Eq;
409
+ ((Eq4) => {
410
+ function convertAnyToString(value) {
411
+ if (typeof value !== "object" || null === value || !("toString" in value) || typeof value.toString !== "function" || value.toString !== Object.prototype.toString) {
412
+ return String(value);
413
+ }
414
+ return JSON.stringify(value);
415
+ }
416
+ Eq4.convertAnyToString = convertAnyToString;
417
+ const _anyFlatEq = createAnyEq("FLAT");
418
+ const _anyShallowEq = createAnyEq("SHALLOW");
419
+ const _anyDeepEq = createAnyEq("DEEP");
420
+ function defaultEq() {
421
+ return _anyDeepEq;
422
+ }
423
+ Eq4.defaultEq = defaultEq;
424
+ Eq4.objectIs = Object.is;
425
+ const _valueOfEq = (v1, v2) => Object.is(v1.valueOf(), v2.valueOf());
426
+ function valueOfEq() {
427
+ return _valueOfEq;
428
+ }
429
+ Eq4.valueOfEq = valueOfEq;
430
+ function dateEq() {
431
+ return _valueOfEq;
432
+ }
433
+ Eq4.dateEq = dateEq;
434
+ function createIterableEq(itemEq) {
435
+ return (v1, v2) => {
436
+ if (Object.is(v1, v2))
437
+ return true;
438
+ const iter1 = v1[Symbol.iterator]();
439
+ const iter2 = v2[Symbol.iterator]();
440
+ while (true) {
441
+ const value1 = iter1.next();
442
+ const value2 = iter2.next();
443
+ if (value1.done || value2.done)
444
+ return value1.done === value2.done;
445
+ if (!itemEq(value1.value, value2.value))
446
+ return false;
447
+ }
448
+ };
449
+ }
450
+ const _iterableAnyEq = createIterableEq(defaultEq());
451
+ function iterableEq(itemEq) {
452
+ if (void 0 === itemEq)
453
+ return _iterableAnyEq;
454
+ return createIterableEq(itemEq);
455
+ }
456
+ Eq4.iterableEq = iterableEq;
457
+ function createObjectEq(valueEq) {
458
+ return (v1, v2) => {
459
+ if (Object.is(v1, v2))
460
+ return true;
461
+ if (v1.constructor !== v2.constructor)
462
+ return false;
463
+ for (const key in v1) {
464
+ if (!(key in v2))
465
+ return false;
466
+ }
467
+ for (const key in v2) {
468
+ if (!(key in v1))
469
+ return false;
470
+ }
471
+ for (const key in v1) {
472
+ const value1 = v1[key];
473
+ const value2 = v2[key];
474
+ if (!valueEq(value1, value2))
475
+ return false;
476
+ }
477
+ return true;
478
+ };
479
+ }
480
+ const _objectEq = createObjectEq(defaultEq());
481
+ function objectEq(valueEq) {
482
+ if (void 0 === valueEq)
483
+ return _objectEq;
484
+ return createObjectEq(valueEq);
485
+ }
486
+ Eq4.objectEq = objectEq;
487
+ function createAnyEq(mode) {
488
+ const result = (v1, v2) => {
489
+ if (Object.is(v1, v2))
490
+ return true;
491
+ const type1 = typeof v1;
492
+ const type2 = typeof v2;
493
+ if (type1 !== type2)
494
+ return false;
495
+ switch (type1) {
496
+ case "undefined":
497
+ case "bigint":
498
+ case "boolean":
499
+ case "number":
500
+ case "string":
501
+ case "symbol":
502
+ case "function":
503
+ return Object.is(v1, v2);
504
+ case "object": {
505
+ if (v1 === null || v2 === null)
506
+ return false;
507
+ if (v1.constructor !== v2.constructor) {
508
+ return false;
509
+ }
510
+ if (v1 instanceof Boolean || v1 instanceof Date || v1 instanceof Number || v1 instanceof String) {
511
+ return _valueOfEq(v1, v2);
512
+ }
513
+ if (mode !== "FLAT") {
514
+ if (Symbol.iterator in v1 && Symbol.iterator in v2) {
515
+ if (mode === "SHALLOW") {
516
+ return createIterableEq(_anyFlatEq)(v1, v2);
517
+ }
518
+ return createIterableEq(result)(v1, v2);
519
+ }
520
+ if (mode === "SHALLOW") {
521
+ return createObjectEq(_anyFlatEq)(v1, v2);
522
+ }
523
+ return _objectEq(v1, v2);
524
+ }
525
+ return false;
526
+ }
527
+ }
528
+ };
529
+ return result;
530
+ }
531
+ function anyFlatEq() {
532
+ return _anyFlatEq;
533
+ }
534
+ Eq4.anyFlatEq = anyFlatEq;
535
+ function anyShallowEq() {
536
+ return _anyShallowEq;
537
+ }
538
+ Eq4.anyShallowEq = anyShallowEq;
539
+ function anyDeepEq() {
540
+ return _anyDeepEq;
541
+ }
542
+ Eq4.anyDeepEq = anyDeepEq;
543
+ const _defaultCollator = Intl.Collator("und");
544
+ const _defaultStringCollatorEq = (v1, v2) => _defaultCollator.compare(v1, v2) === 0;
545
+ function createStringCollatorEq(...args) {
546
+ if (args.length === 0)
547
+ return _defaultStringCollatorEq;
548
+ const collator = Intl.Collator(...args);
549
+ return (v1, v2) => collator.compare(v1, v2) === 0;
550
+ }
551
+ Eq4.createStringCollatorEq = createStringCollatorEq;
552
+ const _stringCaseInsensitiveEq = createStringCollatorEq("und", {
553
+ sensitivity: "accent"
554
+ });
555
+ function stringCaseInsentitiveEq() {
556
+ return _stringCaseInsensitiveEq;
557
+ }
558
+ Eq4.stringCaseInsentitiveEq = stringCaseInsentitiveEq;
559
+ const _stringCharCodeEq = (v1, v2) => {
560
+ const len = v1.length;
561
+ if (len !== v2.length)
562
+ return false;
563
+ let i = -1;
564
+ while (++i < len) {
565
+ if (v1.charCodeAt(i) !== v2.charCodeAt(i))
566
+ return false;
567
+ }
568
+ return true;
569
+ };
570
+ function stringCharCodeEq() {
571
+ return _stringCharCodeEq;
572
+ }
573
+ Eq4.stringCharCodeEq = stringCharCodeEq;
574
+ const _anyToStringEq = (v1, v2) => convertAnyToString(v1) === convertAnyToString(v2);
575
+ function anyToStringEq() {
576
+ return _anyToStringEq;
577
+ }
578
+ Eq4.anyToStringEq = anyToStringEq;
579
+ const _anyJsonEq = (v1, v2) => JSON.stringify(v1) === JSON.stringify(v2);
580
+ function anyJsonEq() {
581
+ return _anyJsonEq;
582
+ }
583
+ Eq4.anyJsonEq = anyJsonEq;
584
+ function tupleSymmetric(eq = defaultEq()) {
585
+ return (tup1, tup2) => eq(tup1[0], tup2[0]) && eq(tup1[1], tup2[1]) || eq(tup1[0], tup2[1]) && eq(tup1[1], tup2[0]);
586
+ }
587
+ Eq4.tupleSymmetric = tupleSymmetric;
588
+ })(Eq || (Eq = {}));
589
+
590
+ // src/err.mts
591
+ function Err() {
592
+ return ErrBase.msg("Err: Forced to throw error")();
593
+ }
594
+ var ErrBase;
595
+ ((ErrBase2) => {
596
+ class CustomError {
597
+ constructor(message) {
598
+ this.message = message;
599
+ }
600
+ get name() {
601
+ return this.constructor.name;
602
+ }
603
+ }
604
+ ErrBase2.CustomError = CustomError;
605
+ class ForcedError extends CustomError {
606
+ }
607
+ ErrBase2.ForcedError = ForcedError;
608
+ function msg(message) {
609
+ return function() {
610
+ throw new ErrBase2.ForcedError(message);
611
+ };
612
+ }
613
+ ErrBase2.msg = msg;
614
+ })(ErrBase || (ErrBase = {}));
615
+
616
+ // src/index-range.mts
617
+ var IndexRange;
618
+ ((IndexRange2) => {
619
+ function getIndexRangeIndices(range) {
620
+ if (void 0 !== range.amount) {
621
+ if (void 0 === range.start)
622
+ return [0, range.amount - 1];
623
+ if (Array.isArray(range.start)) {
624
+ const [start2, includeStart] = range.start;
625
+ if (includeStart)
626
+ return [start2, start2 + range.amount - 1];
627
+ return [start2 + 1, start2 + 1 + range.amount - 1];
628
+ }
629
+ return [range.start, range.start + range.amount - 1];
630
+ }
631
+ let start = 0;
632
+ let end = void 0;
633
+ if (`start` in range) {
634
+ if (Array.isArray(range.start)) {
635
+ if (range.start[1])
636
+ start = range.start[0];
637
+ else
638
+ start = range.start[0] + 1;
639
+ } else
640
+ start = range.start ?? 0;
641
+ }
642
+ if (`end` in range) {
643
+ if (Array.isArray(range.end)) {
644
+ if (range.end[1])
645
+ end = range.end[0];
646
+ else
647
+ end = range.end[0] - 1;
648
+ } else
649
+ end = range.end;
650
+ }
651
+ return [start, end];
652
+ }
653
+ IndexRange2.getIndexRangeIndices = getIndexRangeIndices;
654
+ function getIndicesFor(range, length) {
655
+ if (length <= 0)
656
+ return "empty";
657
+ let start = 0;
658
+ let end = length - 1;
659
+ if (void 0 !== range.start) {
660
+ if (Array.isArray(range.start)) {
661
+ start = range.start[0];
662
+ if (!range.start[1])
663
+ start++;
664
+ } else
665
+ start = range.start;
666
+ if (start >= length || -start > length)
667
+ return "empty";
668
+ if (start < 0)
669
+ start = length + start;
670
+ }
671
+ if (void 0 !== range.amount) {
672
+ if (range.amount <= 0)
673
+ return "empty";
674
+ if (void 0 === range.start) {
675
+ if (range.amount >= length)
676
+ return "all";
677
+ return [0, Math.min(end, range.amount - 1)];
678
+ }
679
+ end = start + range.amount - 1;
680
+ } else if (void 0 !== range.end) {
681
+ if (Array.isArray(range.end)) {
682
+ end = range.end[0];
683
+ if (!range.end[1]) {
684
+ if (end === 0)
685
+ return "empty";
686
+ end--;
687
+ }
688
+ } else
689
+ end = range.end;
690
+ if (end < 0)
691
+ end = length + end;
692
+ }
693
+ if (end < start)
694
+ return "empty";
695
+ end = Math.min(length - 1, end);
696
+ if (start === 0 && end === length - 1)
697
+ return "all";
698
+ return [start, end];
699
+ }
700
+ IndexRange2.getIndicesFor = getIndicesFor;
701
+ })(IndexRange || (IndexRange = {}));
702
+
703
+ // src/optlazy.mts
704
+ function OptLazy(optLazy) {
705
+ if (optLazy instanceof Function)
706
+ return optLazy();
707
+ return optLazy;
708
+ }
709
+ function OptLazyOr(optLazyOr, otherValue) {
710
+ if (optLazyOr instanceof Function)
711
+ return optLazyOr(otherValue);
712
+ return optLazyOr;
713
+ }
714
+
715
+ // src/range.mts
716
+ var Range;
717
+ ((Range2) => {
718
+ function getNormalizedRange(range) {
719
+ let start = void 0;
720
+ let end = void 0;
721
+ if (`start` in range && void 0 !== range.start) {
722
+ if (Array.isArray(range.start) && range.start.length === 2 && typeof range.start[1] === "boolean") {
723
+ start = range.start;
724
+ } else {
725
+ start = [range.start, true];
726
+ }
727
+ }
728
+ if (`end` in range && void 0 !== range.end) {
729
+ if (Array.isArray(range.end) && range.end.length === 2 && typeof range.end[1] === "boolean") {
730
+ end = range.end;
731
+ } else {
732
+ end = [range.end, true];
733
+ }
734
+ }
735
+ return { start, end };
736
+ }
737
+ Range2.getNormalizedRange = getNormalizedRange;
738
+ })(Range || (Range = {}));
739
+
740
+ // src/reducer.mts
741
+ function identity(value) {
742
+ return value;
743
+ }
744
+ var Reducer;
745
+ ((Reducer3) => {
746
+ class Base {
747
+ constructor(init, next, stateToResult) {
748
+ this.init = init;
749
+ this.next = next;
750
+ this.stateToResult = stateToResult;
751
+ }
752
+ filterInput(pred) {
753
+ return create(
754
+ () => ({
755
+ nextIndex: 0,
756
+ state: OptLazy(this.init)
757
+ }),
758
+ (state, elem, index, halt) => {
759
+ if (pred(elem, index, halt)) {
760
+ state.state = this.next(state.state, elem, state.nextIndex++, halt);
761
+ }
762
+ return state;
763
+ },
764
+ (state) => this.stateToResult(state.state)
765
+ );
766
+ }
767
+ mapInput(mapFun) {
768
+ return create(
769
+ this.init,
770
+ (state, elem, index, halt) => this.next(state, mapFun(elem, index), index, halt),
771
+ this.stateToResult
772
+ );
773
+ }
774
+ collectInput(collectFun) {
775
+ return create(
776
+ () => ({
777
+ nextIndex: 0,
778
+ state: OptLazy(this.init)
779
+ }),
780
+ (state, elem, index, halt) => {
781
+ const nextElem = collectFun(elem, index, CollectFun.Skip, halt);
782
+ if (CollectFun.Skip !== nextElem) {
783
+ state.state = this.next(
784
+ state.state,
785
+ nextElem,
786
+ state.nextIndex++,
787
+ halt
788
+ );
789
+ }
790
+ return state;
791
+ },
792
+ (state) => this.stateToResult(state.state)
793
+ );
794
+ }
795
+ mapOutput(mapFun) {
796
+ return create(
797
+ this.init,
798
+ this.next,
799
+ (state) => mapFun(this.stateToResult(state))
800
+ );
801
+ }
802
+ takeInput(amount) {
803
+ if (amount <= 0) {
804
+ return create(this.init, identity, this.stateToResult);
805
+ }
806
+ return this.filterInput((_, i, halt) => {
807
+ const more = i < amount;
808
+ if (!more)
809
+ halt();
810
+ return more;
811
+ });
812
+ }
813
+ dropInput(amount) {
814
+ if (amount <= 0)
815
+ return this;
816
+ return this.filterInput((_, i) => i >= amount);
817
+ }
818
+ sliceInput(from = 0, amount) {
819
+ if (void 0 === amount)
820
+ return this.dropInput(from);
821
+ if (amount <= 0)
822
+ return create(this.init, identity, this.stateToResult);
823
+ if (from <= 0)
824
+ return this.takeInput(amount);
825
+ return this.takeInput(amount).dropInput(from);
826
+ }
827
+ }
828
+ Reducer3.Base = Base;
829
+ function create(init, next, stateToResult) {
830
+ return new Reducer3.Base(init, next, stateToResult);
831
+ }
832
+ Reducer3.create = create;
833
+ function createMono(init, next, stateToResult) {
834
+ return create(init, next, stateToResult ?? identity);
835
+ }
836
+ Reducer3.createMono = createMono;
837
+ function createOutput(init, next, stateToResult) {
838
+ return create(init, next, stateToResult ?? identity);
839
+ }
840
+ Reducer3.createOutput = createOutput;
841
+ Reducer3.sum = createMono(0, (state, next) => state + next);
842
+ Reducer3.product = createMono(1, (state, next, _, halt) => {
843
+ if (0 === next)
844
+ halt();
845
+ return state * next;
846
+ });
847
+ Reducer3.average = createMono(
848
+ 0,
849
+ (avg, value, index) => avg + (value - avg) / (index + 1)
850
+ );
851
+ Reducer3.minBy = (compFun, otherwise) => {
852
+ const token = Symbol();
853
+ return create(
854
+ token,
855
+ (state, next) => {
856
+ if (token === state)
857
+ return next;
858
+ return compFun(state, next) < 0 ? state : next;
859
+ },
860
+ (state) => token === state ? OptLazy(otherwise) : state
861
+ );
862
+ };
863
+ Reducer3.min = (otherwise) => {
864
+ return create(
865
+ void 0,
866
+ (state, next) => void 0 !== state && state < next ? state : next,
867
+ (state) => state ?? OptLazy(otherwise)
868
+ );
869
+ };
870
+ Reducer3.maxBy = (compFun, otherwise) => {
871
+ const token = Symbol();
872
+ return create(
873
+ token,
874
+ (state, next) => {
875
+ if (token === state)
876
+ return next;
877
+ return compFun(state, next) > 0 ? state : next;
878
+ },
879
+ (state) => token === state ? OptLazy(otherwise) : state
880
+ );
881
+ };
882
+ Reducer3.max = (otherwise) => {
883
+ return create(
884
+ void 0,
885
+ (state, next) => void 0 !== state && state > next ? state : next,
886
+ (state) => state ?? OptLazy(otherwise)
887
+ );
888
+ };
889
+ function join({
890
+ sep = "",
891
+ start = "",
892
+ end = "",
893
+ valueToString = String
894
+ } = {}) {
895
+ let curSep = "";
896
+ let curStart = start;
897
+ return create(
898
+ "",
899
+ (state, next) => {
900
+ const result = curStart.concat(state, curSep, valueToString(next));
901
+ curSep = sep;
902
+ curStart = "";
903
+ return result;
904
+ },
905
+ (state) => state.concat(end)
906
+ );
907
+ }
908
+ Reducer3.join = join;
909
+ Reducer3.count = (pred) => {
910
+ if (void 0 === pred)
911
+ return createOutput(0, (_, __, i) => i + 1);
912
+ return createOutput(0, (state, next, i) => {
913
+ if (pred?.(next, i))
914
+ return state + 1;
915
+ return state;
916
+ });
917
+ };
918
+ Reducer3.firstWhere = (pred, otherwise) => {
919
+ const token = Symbol();
920
+ return create(
921
+ token,
922
+ (state, next, i, halt) => {
923
+ if (token === state && pred(next, i)) {
924
+ halt();
925
+ return next;
926
+ }
927
+ return state;
928
+ },
929
+ (state) => token === state ? OptLazy(otherwise) : state
930
+ );
931
+ };
932
+ Reducer3.first = (otherwise) => {
933
+ const token = Symbol();
934
+ return create(
935
+ token,
936
+ (state, next, _, halt) => {
937
+ halt();
938
+ if (token === state)
939
+ return next;
940
+ return state;
941
+ },
942
+ (state) => token === state ? OptLazy(otherwise) : state
943
+ );
944
+ };
945
+ Reducer3.lastWhere = (pred, otherwise) => {
946
+ const token = Symbol();
947
+ return create(
948
+ token,
949
+ (state, next, i) => {
950
+ if (pred(next, i))
951
+ return next;
952
+ return state;
953
+ },
954
+ (state) => token === state ? OptLazy(otherwise) : state
955
+ );
956
+ };
957
+ Reducer3.last = (otherwise) => {
958
+ const token = Symbol();
959
+ return create(
960
+ () => token,
961
+ (_, next) => next,
962
+ (state) => token === state ? OptLazy(otherwise) : state
963
+ );
964
+ };
965
+ function some(pred) {
966
+ return createOutput(false, (state, next, i, halt) => {
967
+ if (state)
968
+ return state;
969
+ const satisfies = pred(next, i);
970
+ if (satisfies) {
971
+ halt();
972
+ }
973
+ return satisfies;
974
+ });
975
+ }
976
+ Reducer3.some = some;
977
+ function every(pred) {
978
+ return createOutput(true, (state, next, i, halt) => {
979
+ if (!state)
980
+ return state;
981
+ const satisfies = pred(next, i);
982
+ if (!satisfies) {
983
+ halt();
984
+ }
985
+ return satisfies;
986
+ });
987
+ }
988
+ Reducer3.every = every;
989
+ function contains(elem, eq = Object.is) {
990
+ return createOutput(false, (state, next, _, halt) => {
991
+ if (state)
992
+ return state;
993
+ const satisfies = eq(next, elem);
994
+ if (satisfies) {
995
+ halt();
996
+ }
997
+ return satisfies;
998
+ });
999
+ }
1000
+ Reducer3.contains = contains;
1001
+ Reducer3.and = createMono(true, (state, next, _, halt) => {
1002
+ if (!state)
1003
+ return state;
1004
+ if (!next)
1005
+ halt();
1006
+ return next;
1007
+ });
1008
+ Reducer3.or = createMono(false, (state, next, _, halt) => {
1009
+ if (state)
1010
+ return state;
1011
+ if (next)
1012
+ halt();
1013
+ return next;
1014
+ });
1015
+ Reducer3.isEmpty = createOutput(
1016
+ true,
1017
+ (_, __, ___, halt) => {
1018
+ halt();
1019
+ return false;
1020
+ }
1021
+ );
1022
+ Reducer3.nonEmpty = createOutput(
1023
+ false,
1024
+ (_, __, ___, halt) => {
1025
+ halt();
1026
+ return true;
1027
+ }
1028
+ );
1029
+ function toArray() {
1030
+ return create(
1031
+ () => [],
1032
+ (state, next) => {
1033
+ state.push(next);
1034
+ return state;
1035
+ },
1036
+ (state) => state.slice()
1037
+ );
1038
+ }
1039
+ Reducer3.toArray = toArray;
1040
+ function toJSMap() {
1041
+ return create(
1042
+ () => /* @__PURE__ */ new Map(),
1043
+ (state, next) => {
1044
+ state.set(next[0], next[1]);
1045
+ return state;
1046
+ },
1047
+ (s) => new Map(s)
1048
+ );
1049
+ }
1050
+ Reducer3.toJSMap = toJSMap;
1051
+ function toJSSet() {
1052
+ return create(
1053
+ () => /* @__PURE__ */ new Set(),
1054
+ (state, next) => {
1055
+ state.add(next);
1056
+ return state;
1057
+ },
1058
+ (s) => new Set(s)
1059
+ );
1060
+ }
1061
+ Reducer3.toJSSet = toJSSet;
1062
+ function toJSObject() {
1063
+ return create(
1064
+ () => ({}),
1065
+ (state, entry) => {
1066
+ state[entry[0]] = entry[1];
1067
+ return state;
1068
+ },
1069
+ (s) => ({ ...s })
1070
+ );
1071
+ }
1072
+ Reducer3.toJSObject = toJSObject;
1073
+ function combineArr(...reducers) {
1074
+ const createState = () => {
1075
+ return reducers.map((reducer) => {
1076
+ const result = {
1077
+ reducer,
1078
+ halted: false,
1079
+ halt() {
1080
+ result.halted = true;
1081
+ },
1082
+ state: OptLazy(reducer.init)
1083
+ };
1084
+ return result;
1085
+ });
1086
+ };
1087
+ return create(
1088
+ createState,
1089
+ (allState, next, index, halt) => {
1090
+ let anyNotHalted = false;
1091
+ let i = -1;
1092
+ const len = allState.length;
1093
+ while (++i < len) {
1094
+ const red = allState[i];
1095
+ if (red.halted) {
1096
+ continue;
1097
+ }
1098
+ red.state = red.reducer.next(red.state, next, index, red.halt);
1099
+ if (!red.halted) {
1100
+ anyNotHalted = true;
1101
+ }
1102
+ }
1103
+ if (!anyNotHalted) {
1104
+ halt();
1105
+ }
1106
+ return allState;
1107
+ },
1108
+ (allState) => allState.map((st) => st.reducer.stateToResult(st.state))
1109
+ );
1110
+ }
1111
+ Reducer3.combineArr = combineArr;
1112
+ function combineObj(reducerObj) {
1113
+ const createState = () => {
1114
+ const allState = {};
1115
+ for (const key in reducerObj) {
1116
+ const reducer = reducerObj[key];
1117
+ const result = {
1118
+ reducer,
1119
+ halted: false,
1120
+ halt() {
1121
+ result.halted = true;
1122
+ },
1123
+ state: OptLazy(reducer.init)
1124
+ };
1125
+ allState[key] = result;
1126
+ }
1127
+ return allState;
1128
+ };
1129
+ return create(
1130
+ createState,
1131
+ (allState, next, index, halt) => {
1132
+ let anyNotHalted = false;
1133
+ for (const key in allState) {
1134
+ const red = allState[key];
1135
+ if (red.halted) {
1136
+ continue;
1137
+ }
1138
+ red.state = red.reducer.next(red.state, next, index, red.halt);
1139
+ if (!red.halted) {
1140
+ anyNotHalted = true;
1141
+ }
1142
+ }
1143
+ if (!anyNotHalted) {
1144
+ halt();
1145
+ }
1146
+ return allState;
1147
+ },
1148
+ (allState) => {
1149
+ const result = {};
1150
+ for (const key in allState) {
1151
+ const st = allState[key];
1152
+ result[key] = st.reducer.stateToResult(st.state);
1153
+ }
1154
+ return result;
1155
+ }
1156
+ );
1157
+ }
1158
+ Reducer3.combineObj = combineObj;
1159
+ })(Reducer || (Reducer = {}));
1160
+
1161
+ // src/traverse-state.mts
1162
+ var TraverseStateImpl = class {
1163
+ constructor(startIndex) {
1164
+ this.startIndex = startIndex;
1165
+ this.halted = false;
1166
+ this.halt = () => {
1167
+ this.halted = true;
1168
+ };
1169
+ this.currentIndex = startIndex;
1170
+ }
1171
+ nextIndex() {
1172
+ return this.currentIndex++;
1173
+ }
1174
+ reset() {
1175
+ this.currentIndex = this.startIndex;
1176
+ this.halted = false;
1177
+ }
1178
+ };
1179
+ function TraverseState(startIndex = 0) {
1180
+ return new TraverseStateImpl(startIndex);
1181
+ }
1182
+
1183
+ // src/update.mts
1184
+ function Update(value, update) {
1185
+ if (typeof update === "function") {
1186
+ return update(value);
1187
+ }
1188
+ return update;
1189
+ }
1190
+
1191
+ // src/async-optlazy.mts
1192
+ var AsyncOptLazy;
1193
+ ((AsyncOptLazy2) => {
1194
+ function toMaybePromise(optLazy) {
1195
+ if (optLazy instanceof Function)
1196
+ return optLazy();
1197
+ return optLazy;
1198
+ }
1199
+ AsyncOptLazy2.toMaybePromise = toMaybePromise;
1200
+ async function toPromise(optLazy) {
1201
+ if (optLazy instanceof Function)
1202
+ return optLazy();
1203
+ return optLazy;
1204
+ }
1205
+ AsyncOptLazy2.toPromise = toPromise;
1206
+ })(AsyncOptLazy || (AsyncOptLazy = {}));
1207
+
1208
+ // src/async-reducer.mts
1209
+ function identity2(value) {
1210
+ return value;
1211
+ }
1212
+ var AsyncReducer;
1213
+ ((AsyncReducer2) => {
1214
+ class Base {
1215
+ constructor(init, next, stateToResult, onClose) {
1216
+ this.init = init;
1217
+ this.next = next;
1218
+ this.stateToResult = stateToResult;
1219
+ this.onClose = onClose;
1220
+ }
1221
+ filterInput(pred) {
1222
+ return create(
1223
+ async () => ({
1224
+ nextIndex: 0,
1225
+ state: await AsyncOptLazy.toMaybePromise(this.init)
1226
+ }),
1227
+ async (state, elem, index, halt) => {
1228
+ if (pred(elem, index, halt)) {
1229
+ state.state = await this.next(
1230
+ state.state,
1231
+ elem,
1232
+ state.nextIndex++,
1233
+ halt
1234
+ );
1235
+ }
1236
+ return state;
1237
+ },
1238
+ (state) => this.stateToResult(state.state),
1239
+ (state, error) => this.onClose?.(state.state, error)
1240
+ );
1241
+ }
1242
+ mapInput(mapFun) {
1243
+ return create(
1244
+ this.init,
1245
+ async (state, elem, index, halt) => this.next(state, await mapFun(elem, index), index, halt),
1246
+ this.stateToResult,
1247
+ this.onClose
1248
+ );
1249
+ }
1250
+ collectInput(collectFun) {
1251
+ return create(
1252
+ async () => ({
1253
+ nextIndex: 0,
1254
+ state: await AsyncOptLazy.toMaybePromise(this.init)
1255
+ }),
1256
+ async (state, elem, index, halt) => {
1257
+ const nextElem = await collectFun(elem, index, CollectFun.Skip, halt);
1258
+ if (CollectFun.Skip !== nextElem) {
1259
+ state.state = await this.next(
1260
+ state.state,
1261
+ nextElem,
1262
+ state.nextIndex++,
1263
+ halt
1264
+ );
1265
+ }
1266
+ return state;
1267
+ },
1268
+ (state) => this.stateToResult(state.state),
1269
+ (state, error) => this.onClose?.(state.state, error)
1270
+ );
1271
+ }
1272
+ mapOutput(mapFun) {
1273
+ return create(
1274
+ this.init,
1275
+ this.next,
1276
+ async (state) => mapFun(await this.stateToResult(state)),
1277
+ this.onClose
1278
+ );
1279
+ }
1280
+ takeInput(amount) {
1281
+ if (amount <= 0) {
1282
+ return create(this.init, identity2, this.stateToResult);
1283
+ }
1284
+ return this.filterInput((_, i, halt) => {
1285
+ const more = i < amount;
1286
+ if (!more)
1287
+ halt();
1288
+ return more;
1289
+ });
1290
+ }
1291
+ dropInput(amount) {
1292
+ if (amount <= 0) {
1293
+ return this;
1294
+ }
1295
+ return this.filterInput((_, i) => i >= amount);
1296
+ }
1297
+ sliceInput(from2 = 0, amount) {
1298
+ if (void 0 === amount)
1299
+ return this.dropInput(from2);
1300
+ if (amount <= 0)
1301
+ return create(this.init, identity2, this.stateToResult);
1302
+ if (from2 <= 0)
1303
+ return this.takeInput(amount);
1304
+ return this.takeInput(amount).dropInput(from2);
1305
+ }
1306
+ }
1307
+ AsyncReducer2.Base = Base;
1308
+ function create(init, next, stateToResult, onClose) {
1309
+ return new AsyncReducer2.Base(
1310
+ init,
1311
+ next,
1312
+ stateToResult,
1313
+ onClose
1314
+ );
1315
+ }
1316
+ AsyncReducer2.create = create;
1317
+ function createMono(init, next, stateToResult, onClose) {
1318
+ return create(init, next, stateToResult ?? identity2, onClose);
1319
+ }
1320
+ AsyncReducer2.createMono = createMono;
1321
+ function createOutput(init, next, stateToResult, onClose) {
1322
+ return create(init, next, stateToResult ?? identity2, onClose);
1323
+ }
1324
+ AsyncReducer2.createOutput = createOutput;
1325
+ function from(reducer) {
1326
+ return AsyncReducer2.create(
1327
+ reducer.init,
1328
+ reducer.next,
1329
+ reducer.stateToResult
1330
+ );
1331
+ }
1332
+ AsyncReducer2.from = from;
1333
+ AsyncReducer2.sum = createMono(0, (state, next) => state + next);
1334
+ AsyncReducer2.product = createMono(1, (state, next, _, halt) => {
1335
+ if (0 === next)
1336
+ halt();
1337
+ return state * next;
1338
+ });
1339
+ AsyncReducer2.average = createMono(
1340
+ 0,
1341
+ (avg, value, index) => avg + (value - avg) / (index + 1)
1342
+ );
1343
+ AsyncReducer2.minBy = (compFun, otherwise) => {
1344
+ const token = Symbol();
1345
+ return create(
1346
+ token,
1347
+ async (state, next) => {
1348
+ if (token === state)
1349
+ return next;
1350
+ return await compFun(state, next) < 0 ? state : next;
1351
+ },
1352
+ (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1353
+ );
1354
+ };
1355
+ AsyncReducer2.min = (otherwise) => {
1356
+ return create(
1357
+ void 0,
1358
+ (state, next) => void 0 !== state && state < next ? state : next,
1359
+ (state) => state ?? AsyncOptLazy.toMaybePromise(otherwise)
1360
+ );
1361
+ };
1362
+ AsyncReducer2.maxBy = (compFun, otherwise) => {
1363
+ const token = Symbol();
1364
+ return create(
1365
+ token,
1366
+ async (state, next) => {
1367
+ if (token === state)
1368
+ return next;
1369
+ return await compFun(state, next) > 0 ? state : next;
1370
+ },
1371
+ (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1372
+ );
1373
+ };
1374
+ AsyncReducer2.max = (otherwise) => {
1375
+ return create(
1376
+ void 0,
1377
+ (state, next) => void 0 !== state && state > next ? state : next,
1378
+ (state) => state ?? AsyncOptLazy.toMaybePromise(otherwise)
1379
+ );
1380
+ };
1381
+ function join({
1382
+ sep = "",
1383
+ start = "",
1384
+ end = "",
1385
+ valueToString = String
1386
+ } = {}) {
1387
+ let curSep = "";
1388
+ let curStart = start;
1389
+ return create(
1390
+ "",
1391
+ (state, next) => {
1392
+ const result = curStart.concat(state, curSep, valueToString(next));
1393
+ curSep = sep;
1394
+ curStart = "";
1395
+ return result;
1396
+ },
1397
+ (state) => state.concat(end)
1398
+ );
1399
+ }
1400
+ AsyncReducer2.join = join;
1401
+ AsyncReducer2.count = (pred) => {
1402
+ if (void 0 === pred)
1403
+ return createOutput(0, (_, __, i) => i + 1);
1404
+ return createOutput(0, async (state, next, i) => {
1405
+ if (await pred?.(next, i))
1406
+ return state + 1;
1407
+ return state;
1408
+ });
1409
+ };
1410
+ AsyncReducer2.firstWhere = (pred, otherwise) => {
1411
+ const token = Symbol();
1412
+ return create(
1413
+ token,
1414
+ async (state, next, i, halt) => {
1415
+ if (token === state && await pred(next, i)) {
1416
+ halt();
1417
+ return next;
1418
+ }
1419
+ return state;
1420
+ },
1421
+ (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1422
+ );
1423
+ };
1424
+ AsyncReducer2.first = (otherwise) => {
1425
+ const token = Symbol();
1426
+ return create(
1427
+ token,
1428
+ (state, next, _, halt) => {
1429
+ halt();
1430
+ if (token === state)
1431
+ return next;
1432
+ return state;
1433
+ },
1434
+ (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1435
+ );
1436
+ };
1437
+ AsyncReducer2.lastWhere = (pred, otherwise) => {
1438
+ const token = Symbol();
1439
+ return create(
1440
+ token,
1441
+ async (state, next, i) => {
1442
+ if (await pred(next, i))
1443
+ return next;
1444
+ return state;
1445
+ },
1446
+ (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1447
+ );
1448
+ };
1449
+ AsyncReducer2.last = (otherwise) => {
1450
+ const token = Symbol();
1451
+ return create(
1452
+ () => token,
1453
+ (_, next) => next,
1454
+ (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1455
+ );
1456
+ };
1457
+ function some(pred) {
1458
+ return createOutput(
1459
+ false,
1460
+ async (state, next, i, halt) => {
1461
+ if (state)
1462
+ return state;
1463
+ const satisfies = await pred(next, i);
1464
+ if (satisfies) {
1465
+ halt();
1466
+ }
1467
+ return satisfies;
1468
+ }
1469
+ );
1470
+ }
1471
+ AsyncReducer2.some = some;
1472
+ function every(pred) {
1473
+ return createOutput(
1474
+ true,
1475
+ async (state, next, i, halt) => {
1476
+ if (!state)
1477
+ return state;
1478
+ const satisfies = await pred(next, i);
1479
+ if (!satisfies) {
1480
+ halt();
1481
+ }
1482
+ return satisfies;
1483
+ }
1484
+ );
1485
+ }
1486
+ AsyncReducer2.every = every;
1487
+ function contains(elem, eq = Object.is) {
1488
+ return createOutput(false, (state, next, _, halt) => {
1489
+ if (state)
1490
+ return state;
1491
+ const satisfies = eq(next, elem);
1492
+ if (satisfies) {
1493
+ halt();
1494
+ }
1495
+ return satisfies;
1496
+ });
1497
+ }
1498
+ AsyncReducer2.contains = contains;
1499
+ AsyncReducer2.and = createMono(true, (state, next, _, halt) => {
1500
+ if (!state)
1501
+ return state;
1502
+ if (!next)
1503
+ halt();
1504
+ return next;
1505
+ });
1506
+ AsyncReducer2.or = createMono(false, (state, next, _, halt) => {
1507
+ if (state)
1508
+ return state;
1509
+ if (next)
1510
+ halt();
1511
+ return next;
1512
+ });
1513
+ AsyncReducer2.isEmpty = createOutput(
1514
+ true,
1515
+ (_, __, ___, halt) => {
1516
+ halt();
1517
+ return false;
1518
+ }
1519
+ );
1520
+ AsyncReducer2.nonEmpty = createOutput(
1521
+ false,
1522
+ (_, __, ___, halt) => {
1523
+ halt();
1524
+ return true;
1525
+ }
1526
+ );
1527
+ function toArray() {
1528
+ return create(
1529
+ () => [],
1530
+ (state, next) => {
1531
+ state.push(next);
1532
+ return state;
1533
+ },
1534
+ (state) => state.slice()
1535
+ );
1536
+ }
1537
+ AsyncReducer2.toArray = toArray;
1538
+ function toJSMap() {
1539
+ return create(
1540
+ () => /* @__PURE__ */ new Map(),
1541
+ (state, next) => {
1542
+ state.set(next[0], next[1]);
1543
+ return state;
1544
+ },
1545
+ (s) => new Map(s)
1546
+ );
1547
+ }
1548
+ AsyncReducer2.toJSMap = toJSMap;
1549
+ function toJSSet() {
1550
+ return create(
1551
+ () => /* @__PURE__ */ new Set(),
1552
+ (state, next) => {
1553
+ state.add(next);
1554
+ return state;
1555
+ },
1556
+ (s) => new Set(s)
1557
+ );
1558
+ }
1559
+ AsyncReducer2.toJSSet = toJSSet;
1560
+ function toJSObject() {
1561
+ return create(
1562
+ () => ({}),
1563
+ (state, entry) => {
1564
+ state[entry[0]] = entry[1];
1565
+ return state;
1566
+ },
1567
+ (s) => ({ ...s })
1568
+ );
1569
+ }
1570
+ AsyncReducer2.toJSObject = toJSObject;
1571
+ function combineArr(...reducers) {
1572
+ const createState = () => {
1573
+ return Promise.all(
1574
+ reducers.map(async (reducer) => {
1575
+ const result = {
1576
+ reducer,
1577
+ halted: false,
1578
+ halt() {
1579
+ result.halted = true;
1580
+ },
1581
+ state: await AsyncOptLazy.toMaybePromise(reducer.init)
1582
+ };
1583
+ return result;
1584
+ })
1585
+ );
1586
+ };
1587
+ return create(
1588
+ createState,
1589
+ async (allState, next, index, halt) => {
1590
+ let anyNotHalted = false;
1591
+ await Promise.all(
1592
+ allState.map(async (red) => {
1593
+ if (red.halted)
1594
+ return;
1595
+ red.state = await red.reducer.next(
1596
+ red.state,
1597
+ next,
1598
+ index,
1599
+ red.halt
1600
+ );
1601
+ if (!red.halted)
1602
+ anyNotHalted = true;
1603
+ })
1604
+ );
1605
+ if (!anyNotHalted)
1606
+ halt();
1607
+ return allState;
1608
+ },
1609
+ (allState) => Promise.all(
1610
+ allState.map((st) => st.reducer.stateToResult(st.state))
1611
+ )
1612
+ );
1613
+ }
1614
+ AsyncReducer2.combineArr = combineArr;
1615
+ function combineObj(reducerObj) {
1616
+ const createState = async () => {
1617
+ const entries = await Promise.all(
1618
+ Object.entries(reducerObj).map(async ([key, reducer]) => {
1619
+ const result = {
1620
+ reducer,
1621
+ halted: false,
1622
+ halt() {
1623
+ result.halted = true;
1624
+ },
1625
+ state: await AsyncOptLazy.toMaybePromise(reducer.init)
1626
+ };
1627
+ return [key, result];
1628
+ })
1629
+ );
1630
+ return Object.fromEntries(entries);
1631
+ };
1632
+ return create(
1633
+ createState,
1634
+ async (allState, next, index, halt) => {
1635
+ let anyNotHalted = false;
1636
+ await Promise.all(
1637
+ Object.values(allState).map(async (red) => {
1638
+ if (red.halted) {
1639
+ return;
1640
+ }
1641
+ red.state = await red.reducer.next(
1642
+ red.state,
1643
+ next,
1644
+ index,
1645
+ red.halt
1646
+ );
1647
+ if (!red.halted) {
1648
+ anyNotHalted = true;
1649
+ }
1650
+ })
1651
+ );
1652
+ if (!anyNotHalted) {
1653
+ halt();
1654
+ }
1655
+ return allState;
1656
+ },
1657
+ async (allState) => {
1658
+ const entries = await Promise.all(
1659
+ Object.entries(allState).map(
1660
+ async ([key, st]) => [key, await st.reducer.stateToResult(st.state)]
1661
+ )
1662
+ );
1663
+ return Object.fromEntries(entries);
1664
+ }
1665
+ );
1666
+ }
1667
+ AsyncReducer2.combineObj = combineObj;
1668
+ })(AsyncReducer || (AsyncReducer = {}));
1669
+ // Annotate the CommonJS export names for ESM import in node:
1670
+ 0 && (module.exports = {
1671
+ AsyncOptLazy,
1672
+ AsyncReducer,
1673
+ CollectFun,
1674
+ Comp,
1675
+ Eq,
1676
+ Err,
1677
+ ErrBase,
1678
+ IndexRange,
1679
+ OptLazy,
1680
+ OptLazyOr,
1681
+ Range,
1682
+ Reducer,
1683
+ TraverseState,
1684
+ Update
1685
+ });