@thi.ng/compare 2.5.2 → 2.5.4
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 +1 -1
- package/compose.d.ts +3 -0
- package/compose.js +21 -11
- package/keys.js +3 -12
- package/package.json +2 -2
package/README.md
CHANGED
package/compose.d.ts
CHANGED
|
@@ -4,6 +4,9 @@ import type { Comparator } from "@thi.ng/api";
|
|
|
4
4
|
* long as the current comparator returns 0. In other words, returns the result
|
|
5
5
|
* of the first comparator with non-zero result, or if none does, returns zero.
|
|
6
6
|
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Provides iteration-free fast past paths for up to 4 comparators.
|
|
9
|
+
*
|
|
7
10
|
* @param cmp
|
|
8
11
|
*/
|
|
9
12
|
export declare const composeComparators: <T = any>(cmp: Comparator<T>, ...xs: Comparator<T>[]) => Comparator<T>;
|
package/compose.js
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
const composeComparators = (cmp, ...xs) => {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
const [cmp2, cmp3, cmp4] = xs;
|
|
3
|
+
switch (xs.length) {
|
|
4
|
+
case 0:
|
|
5
|
+
return cmp;
|
|
6
|
+
case 1:
|
|
7
|
+
return (a, b) => cmp(a, b) || cmp2(a, b);
|
|
8
|
+
case 2:
|
|
9
|
+
return (a, b) => cmp(a, b) || cmp2(a, b) || cmp3(a, b);
|
|
10
|
+
case 3:
|
|
11
|
+
return (a, b) => cmp(a, b) || cmp2(a, b) || cmp3(a, b) || cmp4(a, b);
|
|
12
|
+
default: {
|
|
13
|
+
const fns = [cmp, ...xs];
|
|
14
|
+
const n = fns.length;
|
|
15
|
+
return (a, b) => {
|
|
16
|
+
for (let i = 0; i < n; i++) {
|
|
17
|
+
const res = fns[i](a, b);
|
|
18
|
+
if (res !== 0) return res;
|
|
19
|
+
}
|
|
20
|
+
return 0;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
12
23
|
}
|
|
13
|
-
return cmp;
|
|
14
24
|
};
|
|
15
25
|
export {
|
|
16
26
|
composeComparators
|
package/keys.js
CHANGED
|
@@ -7,29 +7,20 @@ function compareByKey(key, cmp = compare) {
|
|
|
7
7
|
function compareByKeys2(a, b, cmpA = compare, cmpB = compare) {
|
|
8
8
|
const ka = __key(a);
|
|
9
9
|
const kb = __key(b);
|
|
10
|
-
return (x, y) =>
|
|
11
|
-
let res = cmpA(ka(x), ka(y));
|
|
12
|
-
return res === 0 ? cmpB(kb(x), kb(y)) : res;
|
|
13
|
-
};
|
|
10
|
+
return (x, y) => cmpA(ka(x), ka(y)) || cmpB(kb(x), kb(y));
|
|
14
11
|
}
|
|
15
12
|
function compareByKeys3(a, b, c, cmpA = compare, cmpB = compare, cmpC = compare) {
|
|
16
13
|
const ka = __key(a);
|
|
17
14
|
const kb = __key(b);
|
|
18
15
|
const kc = __key(c);
|
|
19
|
-
return (x, y) =>
|
|
20
|
-
let res = cmpA(ka(x), ka(y));
|
|
21
|
-
return res === 0 ? (res = cmpB(kb(x), kb(y))) === 0 ? cmpC(kc(x), kc(y)) : res : res;
|
|
22
|
-
};
|
|
16
|
+
return (x, y) => cmpA(ka(x), ka(y)) || cmpB(kb(x), kb(y)) || cmpC(kc(x), kc(y));
|
|
23
17
|
}
|
|
24
18
|
function compareByKeys4(a, b, c, d, cmpA = compare, cmpB = compare, cmpC = compare, cmpD = compare) {
|
|
25
19
|
const ka = __key(a);
|
|
26
20
|
const kb = __key(b);
|
|
27
21
|
const kc = __key(c);
|
|
28
22
|
const kd = __key(d);
|
|
29
|
-
return (x, y) =>
|
|
30
|
-
let res = cmpA(ka(x), ka(y));
|
|
31
|
-
return res === 0 ? (res = cmpB(kb(x), kb(y))) === 0 ? (res = cmpC(kc(x), kc(y))) === 0 ? cmpD(kd(x), kd(y)) : res : res : res;
|
|
32
|
-
};
|
|
23
|
+
return (x, y) => cmpA(ka(x), ka(y)) || cmpB(kb(x), kb(y)) || cmpC(kc(x), kc(y)) || cmpD(kd(x), kd(y));
|
|
33
24
|
}
|
|
34
25
|
export {
|
|
35
26
|
compareByKey,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/compare",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.4",
|
|
4
4
|
"description": "Comparators with support for types implementing the @thi.ng/api/ICompare interface",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -94,5 +94,5 @@
|
|
|
94
94
|
"thi.ng": {
|
|
95
95
|
"alias": "cmp"
|
|
96
96
|
},
|
|
97
|
-
"gitHead": "
|
|
97
|
+
"gitHead": "7a21075f27ba00b28a6eb77e70918a4c7f4e6e68\n"
|
|
98
98
|
}
|