@rimbu/common 2.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -42
- package/dist/bun/err.mts +1 -1
- package/dist/cjs/async-optlazy.cjs +60 -42
- package/dist/cjs/async-optlazy.cjs.map +1 -0
- package/dist/cjs/async-optlazy.d.cts +46 -0
- package/dist/cjs/collect.cjs +9 -31
- package/dist/cjs/collect.cjs.map +1 -0
- package/dist/cjs/collect.d.cts +29 -0
- package/dist/cjs/comp.cjs +518 -707
- package/dist/cjs/comp.cjs.map +1 -0
- package/dist/cjs/comp.d.cts +239 -0
- package/dist/cjs/eq.cjs +375 -203
- package/dist/cjs/eq.cjs.map +1 -0
- package/dist/cjs/err.cjs +57 -50
- package/dist/cjs/err.cjs.map +1 -0
- package/dist/cjs/index-range.cjs +105 -109
- package/dist/cjs/index-range.cjs.map +1 -0
- package/dist/cjs/index-range.d.cts +42 -0
- package/dist/cjs/index.cjs +9 -797
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +6 -0
- package/dist/cjs/internal.cjs +14 -797
- package/dist/cjs/internal.cjs.map +1 -0
- package/dist/cjs/internal.d.cts +11 -0
- package/dist/cjs/optlazy.cjs +43 -37
- package/dist/cjs/optlazy.cjs.map +1 -0
- package/dist/cjs/range.cjs +35 -49
- package/dist/cjs/range.cjs.map +1 -0
- package/dist/cjs/traverse-state.cjs +29 -46
- package/dist/cjs/traverse-state.cjs.map +1 -0
- package/dist/cjs/types.cjs +2 -17
- package/dist/cjs/types.cjs.map +1 -0
- package/dist/cjs/update.cjs +20 -32
- package/dist/cjs/update.cjs.map +1 -0
- package/dist/esm/eq.d.mts +196 -0
- package/dist/esm/err.d.mts +33 -0
- package/dist/esm/optlazy.d.mts +42 -0
- package/dist/esm/range.d.mts +35 -0
- package/dist/esm/traverse-state.d.mts +33 -0
- package/dist/esm/types.d.mts +31 -0
- package/dist/esm/update.d.mts +18 -0
- package/package.json +19 -12
- package/src/err.mts +1 -1
- /package/dist/{types/eq.d.mts → cjs/eq.d.cts} +0 -0
- /package/dist/{types/err.d.mts → cjs/err.d.cts} +0 -0
- /package/dist/{types/optlazy.d.mts → cjs/optlazy.d.cts} +0 -0
- /package/dist/{types/range.d.mts → cjs/range.d.cts} +0 -0
- /package/dist/{types/traverse-state.d.mts → cjs/traverse-state.d.cts} +0 -0
- /package/dist/{types/types.d.mts → cjs/types.d.cts} +0 -0
- /package/dist/{types/update.d.mts → cjs/update.d.cts} +0 -0
- /package/dist/{types → esm}/async-optlazy.d.mts +0 -0
- /package/dist/{types → esm}/collect.d.mts +0 -0
- /package/dist/{types → esm}/comp.d.mts +0 -0
- /package/dist/{types → esm}/index-range.d.mts +0 -0
- /package/dist/{types → esm}/index.d.mts +0 -0
- /package/dist/{types → esm}/internal.d.mts +0 -0
package/dist/cjs/comp.cjs
CHANGED
|
@@ -1,731 +1,542 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
((Eq2) => {
|
|
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);
|
|
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;
|
|
39
16
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
Eq2.defaultEq = defaultEq;
|
|
50
|
-
Eq2.objectIs = Object.is;
|
|
51
|
-
const _valueOfEq = (v1, v2) => Object.is(v1.valueOf(), v2.valueOf());
|
|
52
|
-
function valueOfEq() {
|
|
53
|
-
return _valueOfEq;
|
|
54
|
-
}
|
|
55
|
-
Eq2.valueOfEq = valueOfEq;
|
|
56
|
-
function dateEq() {
|
|
57
|
-
return _valueOfEq;
|
|
58
|
-
}
|
|
59
|
-
Eq2.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
|
-
Eq2.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
|
-
Eq2.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);
|
|
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 (
|
|
147
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return _anyDeepEq;
|
|
167
|
-
}
|
|
168
|
-
Eq2.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
|
-
Eq2.createStringCollatorEq = createStringCollatorEq;
|
|
178
|
-
const _stringCaseInsensitiveEq = createStringCollatorEq("und", {
|
|
179
|
-
sensitivity: "accent"
|
|
180
|
-
});
|
|
181
|
-
function stringCaseInsentitiveEq() {
|
|
182
|
-
return _stringCaseInsensitiveEq;
|
|
183
|
-
}
|
|
184
|
-
Eq2.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;
|
|
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;
|
|
193
54
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
// src/err.mts
|
|
217
|
-
var ErrBase;
|
|
218
|
-
((ErrBase2) => {
|
|
219
|
-
class CustomError {
|
|
220
|
-
constructor(message) {
|
|
221
|
-
this.message = message;
|
|
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
|
+
},
|
|
63
|
+
};
|
|
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;
|
|
222
77
|
}
|
|
223
|
-
|
|
224
|
-
|
|
78
|
+
Comp.booleanComp = booleanComp;
|
|
79
|
+
var _bigIntComp = {
|
|
80
|
+
isComparable: function (obj) {
|
|
81
|
+
return typeof obj === 'bigint';
|
|
82
|
+
},
|
|
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;
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Returns a default bigint Comp instance that orders bigint numbers naturally.
|
|
94
|
+
*/
|
|
95
|
+
function bigIntComp() {
|
|
96
|
+
return _bigIntComp;
|
|
225
97
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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,
|
|
105
|
+
};
|
|
106
|
+
var _anyStringJSONComp = {
|
|
107
|
+
isComparable: function (obj) {
|
|
108
|
+
return true;
|
|
109
|
+
},
|
|
110
|
+
compare: function (v1, v2) {
|
|
111
|
+
return _defaultCollator.compare(JSON.stringify(v1), JSON.stringify(v2));
|
|
112
|
+
},
|
|
234
113
|
};
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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];
|
|
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;
|
|
253
119
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
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];
|
|
130
|
+
}
|
|
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
|
+
};
|
|
264
140
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
} else
|
|
272
|
-
end = range.end;
|
|
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' });
|
|
273
147
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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;
|
|
293
169
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
return "empty";
|
|
309
|
-
end--;
|
|
310
|
-
}
|
|
311
|
-
} else
|
|
312
|
-
end = range.end;
|
|
313
|
-
if (end < 0)
|
|
314
|
-
end = length + end;
|
|
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;
|
|
315
184
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
(
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
if (`start` in range && void 0 !== range.start) {
|
|
333
|
-
if (Array.isArray(range.start) && range.start.length === 2 && typeof range.start[1] === "boolean") {
|
|
334
|
-
start = range.start;
|
|
335
|
-
} else {
|
|
336
|
-
start = [range.start, true];
|
|
337
|
-
}
|
|
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
|
+
};
|
|
338
201
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
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;
|
|
345
209
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
(
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
var Comp;
|
|
370
|
-
((Comp2) => {
|
|
371
|
-
const _anyFlatComp = createAnyComp("FLAT");
|
|
372
|
-
const _anyShallowComp = createAnyComp("SHALLOW");
|
|
373
|
-
const _anyDeepComp = createAnyComp("DEEP");
|
|
374
|
-
function defaultComp() {
|
|
375
|
-
return _anyDeepComp;
|
|
376
|
-
}
|
|
377
|
-
Comp2.defaultComp = defaultComp;
|
|
378
|
-
const _numberComp = {
|
|
379
|
-
isComparable(obj) {
|
|
380
|
-
return typeof obj === "number";
|
|
381
|
-
},
|
|
382
|
-
compare(v1, v2) {
|
|
383
|
-
if (Number.isFinite(v1) && Number.isFinite(v2)) {
|
|
384
|
-
return v1 - v2;
|
|
385
|
-
}
|
|
386
|
-
if (Number.isNaN(v1)) {
|
|
387
|
-
if (Number.isNaN(v2))
|
|
388
|
-
return 0;
|
|
389
|
-
if (v2 === Number.POSITIVE_INFINITY)
|
|
390
|
-
return 1;
|
|
391
|
-
if (v2 === Number.NEGATIVE_INFINITY)
|
|
392
|
-
return -1;
|
|
393
|
-
return -1;
|
|
394
|
-
}
|
|
395
|
-
if (v1 === Number.POSITIVE_INFINITY) {
|
|
396
|
-
return v2 === Number.POSITIVE_INFINITY ? 0 : 1;
|
|
397
|
-
}
|
|
398
|
-
return v2 === Number.NEGATIVE_INFINITY ? 0 : -1;
|
|
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);
|
|
216
|
+
},
|
|
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
|
+
}
|
|
231
|
+
},
|
|
232
|
+
};
|
|
399
233
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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);
|
|
411
251
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
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);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return undefined;
|
|
428
272
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
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
|
+
};
|
|
447
308
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
if (diff !== 0)
|
|
479
|
-
return diff;
|
|
480
|
-
}
|
|
481
|
-
return v1.length - v2.length;
|
|
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);
|
|
482
339
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
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
|
+
};
|
|
497
394
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
};
|
|
512
|
-
}
|
|
513
|
-
Comp2.createValueOfComp = createValueOfComp;
|
|
514
|
-
const _DateComp = createValueOfComp(Date, _numberComp);
|
|
515
|
-
function dateComp() {
|
|
516
|
-
return _DateComp;
|
|
517
|
-
}
|
|
518
|
-
Comp2.dateComp = dateComp;
|
|
519
|
-
function createIterableComp(itemComp) {
|
|
520
|
-
return {
|
|
521
|
-
isComparable(obj) {
|
|
522
|
-
return typeof obj === "object" && obj !== null && Symbol.iterator in obj;
|
|
523
|
-
},
|
|
524
|
-
compare(v1, v2) {
|
|
525
|
-
const iter1 = v1[Symbol.iterator]();
|
|
526
|
-
const iter2 = v2[Symbol.iterator]();
|
|
527
|
-
while (true) {
|
|
528
|
-
const value1 = iter1.next();
|
|
529
|
-
const value2 = iter2.next();
|
|
530
|
-
if (value1.done)
|
|
531
|
-
return value2.done ? 0 : -1;
|
|
532
|
-
if (value2.done)
|
|
533
|
-
return 1;
|
|
534
|
-
const result = itemComp.compare(value1.value, value2.value);
|
|
535
|
-
if (result !== 0)
|
|
536
|
-
return result;
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
};
|
|
540
|
-
}
|
|
541
|
-
const _iterableAnyComp = createIterableComp(
|
|
542
|
-
Comp2.defaultComp()
|
|
543
|
-
);
|
|
544
|
-
function iterableComp(itemComp) {
|
|
545
|
-
if (void 0 === itemComp)
|
|
546
|
-
return _iterableAnyComp;
|
|
547
|
-
return createIterableComp(itemComp);
|
|
548
|
-
}
|
|
549
|
-
Comp2.iterableComp = iterableComp;
|
|
550
|
-
const _BooleanComp = createValueOfComp(Boolean, _booleanComp);
|
|
551
|
-
const _NumberComp = createValueOfComp(Number, _numberComp);
|
|
552
|
-
const _StringComp = createValueOfComp(String, _stringComp);
|
|
553
|
-
const _wrappedComps = [
|
|
554
|
-
_BooleanComp,
|
|
555
|
-
_DateComp,
|
|
556
|
-
_NumberComp,
|
|
557
|
-
_StringComp
|
|
558
|
-
];
|
|
559
|
-
function tryWrappedComp(v1, v2) {
|
|
560
|
-
let i = -1;
|
|
561
|
-
const len = _wrappedComps.length;
|
|
562
|
-
while (++i < len) {
|
|
563
|
-
const comp = _wrappedComps[i];
|
|
564
|
-
if (comp.isComparable(v1) && comp.isComparable(v2)) {
|
|
565
|
-
return comp.compare(v1, v2);
|
|
566
|
-
}
|
|
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;
|
|
567
408
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
return
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
return _bigIntComp.compare(v1, v2);
|
|
627
|
-
case "boolean":
|
|
628
|
-
return _booleanComp.compare(v1, v2);
|
|
629
|
-
case "number":
|
|
630
|
-
return _numberComp.compare(v1, v2);
|
|
631
|
-
case "string":
|
|
632
|
-
return _stringComp.compare(v1, v2);
|
|
633
|
-
case "object": {
|
|
634
|
-
if (null === v1) {
|
|
635
|
-
if (null === v2)
|
|
636
|
-
return 0;
|
|
637
|
-
return -1;
|
|
638
|
-
}
|
|
639
|
-
if (null === v2) {
|
|
640
|
-
return 1;
|
|
641
|
-
}
|
|
642
|
-
const wrappedComp = tryWrappedComp(v1, v2);
|
|
643
|
-
if (void 0 !== wrappedComp)
|
|
644
|
-
return wrappedComp;
|
|
645
|
-
if (mode !== "FLAT") {
|
|
646
|
-
if (_iterableAnyComp.isComparable(v1) && _iterableAnyComp.isComparable(v2)) {
|
|
647
|
-
if (mode === "SHALLOW") {
|
|
648
|
-
return iterableComp(_anyFlatComp).compare(v1, v2);
|
|
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;
|
|
649
467
|
}
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
}
|
|
726
|
-
Comp2.toEq = toEq;
|
|
727
|
-
})(Comp || (Comp = {}));
|
|
728
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
729
|
-
0 && (module.exports = {
|
|
730
|
-
Comp
|
|
731
|
-
});
|
|
468
|
+
if (undefined === v2)
|
|
469
|
+
return 1;
|
|
470
|
+
return comp.compare(v1, v2);
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
}
|
|
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
|
+
};
|
|
504
|
+
}
|
|
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
|
+
};
|
|
525
|
+
}
|
|
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
|