@thi.ng/compare 2.5.2 → 2.5.3

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 (3) hide show
  1. package/compose.d.ts +3 -0
  2. package/compose.js +21 -11
  3. package/package.json +2 -2
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
- if (xs.length) {
3
- const fns = [cmp, ...xs];
4
- const n = fns.length;
5
- return (a, b) => {
6
- for (let i = 0; i < n; i++) {
7
- const res = fns[i](a, b);
8
- if (res !== 0) return res;
9
- }
10
- return 0;
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/compare",
3
- "version": "2.5.2",
3
+ "version": "2.5.3",
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": "411adbeeb5c03da1cf0c86e567c80942a6b875e7\n"
97
+ "gitHead": "869c807f9230120074f4ef5b9dce651ad0b846b9\n"
98
98
  }