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