@thi.ng/compare 2.2.7 → 2.2.9
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/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/compare.js +20 -17
- package/keys.js +33 -37
- package/numeric.js +6 -14
- package/ops.js +43 -88
- package/package.json +8 -5
- package/reverse.js +4 -7
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
package/compare.js
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
const compare = (a, b) => {
|
|
2
|
+
if (a === b) {
|
|
3
|
+
return 0;
|
|
4
|
+
}
|
|
5
|
+
if (a == null) {
|
|
6
|
+
return b == null ? 0 : -1;
|
|
7
|
+
}
|
|
8
|
+
if (b == null) {
|
|
9
|
+
return a == null ? 0 : 1;
|
|
10
|
+
}
|
|
11
|
+
if (typeof a.compare === "function") {
|
|
12
|
+
return a.compare(b);
|
|
13
|
+
}
|
|
14
|
+
if (typeof b.compare === "function") {
|
|
15
|
+
return -b.compare(a);
|
|
16
|
+
}
|
|
17
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
18
|
+
};
|
|
19
|
+
export {
|
|
20
|
+
compare
|
|
18
21
|
};
|
package/keys.js
CHANGED
|
@@ -1,43 +1,39 @@
|
|
|
1
1
|
import { compare } from "./compare.js";
|
|
2
2
|
const getKey = (k) => typeof k === "function" ? k : (x) => x[k];
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
function compareByKey(a, cmp = compare) {
|
|
4
|
+
const k = getKey(a);
|
|
5
|
+
return (x, y) => cmp(k(x), k(y));
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
function compareByKeys2(a, b, cmpA = compare, cmpB = compare) {
|
|
8
|
+
const ka = getKey(a);
|
|
9
|
+
const kb = getKey(b);
|
|
10
|
+
return (x, y) => {
|
|
11
|
+
let res = cmpA(ka(x), ka(y));
|
|
12
|
+
return res === 0 ? cmpB(kb(x), kb(y)) : res;
|
|
13
|
+
};
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
? cmpC(kc(x), kc(y))
|
|
24
|
-
: res
|
|
25
|
-
: res;
|
|
26
|
-
};
|
|
15
|
+
function compareByKeys3(a, b, c, cmpA = compare, cmpB = compare, cmpC = compare) {
|
|
16
|
+
const ka = getKey(a);
|
|
17
|
+
const kb = getKey(b);
|
|
18
|
+
const kc = getKey(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
|
+
};
|
|
27
23
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
? (res = cmpC(kc(x), kc(y))) === 0
|
|
38
|
-
? cmpD(kd(x), kd(y))
|
|
39
|
-
: res
|
|
40
|
-
: res
|
|
41
|
-
: res;
|
|
42
|
-
};
|
|
24
|
+
function compareByKeys4(a, b, c, d, cmpA = compare, cmpB = compare, cmpC = compare, cmpD = compare) {
|
|
25
|
+
const ka = getKey(a);
|
|
26
|
+
const kb = getKey(b);
|
|
27
|
+
const kc = getKey(c);
|
|
28
|
+
const kd = getKey(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
|
+
};
|
|
43
33
|
}
|
|
34
|
+
export {
|
|
35
|
+
compareByKey,
|
|
36
|
+
compareByKeys2,
|
|
37
|
+
compareByKeys3,
|
|
38
|
+
compareByKeys4
|
|
39
|
+
};
|
package/numeric.js
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export const compareNumAsc = (a, b) => a - b;
|
|
8
|
-
/**
|
|
9
|
-
* Numeric comparator (descending order)
|
|
10
|
-
*
|
|
11
|
-
* @param a -
|
|
12
|
-
* @param b -
|
|
13
|
-
*/
|
|
14
|
-
export const compareNumDesc = (a, b) => b - a;
|
|
1
|
+
const compareNumAsc = (a, b) => a - b;
|
|
2
|
+
const compareNumDesc = (a, b) => b - a;
|
|
3
|
+
export {
|
|
4
|
+
compareNumAsc,
|
|
5
|
+
compareNumDesc
|
|
6
|
+
};
|
package/ops.js
CHANGED
|
@@ -1,99 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
const OPERATORS = {
|
|
2
|
+
"=": eq,
|
|
3
|
+
"!=": neq,
|
|
4
|
+
"<": lt,
|
|
5
|
+
"<=": lte,
|
|
6
|
+
">=": gte,
|
|
7
|
+
">": gt
|
|
8
8
|
};
|
|
9
9
|
const __ensure = (op) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
if (typeof op === "string") {
|
|
11
|
+
if (op in OPERATORS)
|
|
12
|
+
return OPERATORS[op];
|
|
13
|
+
else
|
|
14
|
+
throw new Error(`invalid operator: ${op}`);
|
|
15
|
+
}
|
|
16
|
+
return op;
|
|
17
17
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
* arg (i.e. the LHS of the operator) and always coerces it to a string before
|
|
22
|
-
* applying the operator.
|
|
23
|
-
*
|
|
24
|
-
* @remarks
|
|
25
|
-
* See {@link numericOp} for related functionality.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```ts
|
|
29
|
-
* const equalsABC = stringOp("=", "abc");
|
|
30
|
-
*
|
|
31
|
-
* ["xyz", "abc", "def"].map(equalsABC)
|
|
32
|
-
* // [ false, true, false ]
|
|
33
|
-
*
|
|
34
|
-
* class X {
|
|
35
|
-
* constructor(public body: string) {}
|
|
36
|
-
*
|
|
37
|
-
* toString() {
|
|
38
|
-
* return this.body;
|
|
39
|
-
* }
|
|
40
|
-
* }
|
|
41
|
-
*
|
|
42
|
-
* equalsABC(new X("abc"))
|
|
43
|
-
* // true
|
|
44
|
-
* ```
|
|
45
|
-
*
|
|
46
|
-
* @param op
|
|
47
|
-
* @param x
|
|
48
|
-
*/
|
|
49
|
-
export const stringOp = (op, x) => {
|
|
50
|
-
const impl = __ensure(op);
|
|
51
|
-
return (y) => impl(String(y), x);
|
|
18
|
+
const stringOp = (op, x) => {
|
|
19
|
+
const impl = __ensure(op);
|
|
20
|
+
return (y) => impl(String(y), x);
|
|
52
21
|
};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
* a new function for given operator which only takes a single arg (i.e. the LHS
|
|
57
|
-
* of the operator) and then checks if that arg is a number before applying the
|
|
58
|
-
* operator. For non-numeric args, the returned function will always return
|
|
59
|
-
* false.
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```ts
|
|
63
|
-
* const lessThan42 = numericOp("<", 42);
|
|
64
|
-
*
|
|
65
|
-
* lessThan42(41)
|
|
66
|
-
* // true
|
|
67
|
-
*
|
|
68
|
-
* lessThan42("41")
|
|
69
|
-
* // false
|
|
70
|
-
*
|
|
71
|
-
* lessThan42([41])
|
|
72
|
-
* // false
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* @param op
|
|
76
|
-
* @param x
|
|
77
|
-
*/
|
|
78
|
-
export const numericOp = (op, x) => {
|
|
79
|
-
const impl = __ensure(op);
|
|
80
|
-
return (y) => typeof y === "number" && impl(y, x);
|
|
22
|
+
const numericOp = (op, x) => {
|
|
23
|
+
const impl = __ensure(op);
|
|
24
|
+
return (y) => typeof y === "number" && impl(y, x);
|
|
81
25
|
};
|
|
82
|
-
|
|
83
|
-
|
|
26
|
+
function eq(a, b) {
|
|
27
|
+
return a === b;
|
|
84
28
|
}
|
|
85
|
-
|
|
86
|
-
|
|
29
|
+
function neq(a, b) {
|
|
30
|
+
return a !== b;
|
|
87
31
|
}
|
|
88
|
-
|
|
89
|
-
|
|
32
|
+
function lt(a, b) {
|
|
33
|
+
return a < b;
|
|
90
34
|
}
|
|
91
|
-
|
|
92
|
-
|
|
35
|
+
function lte(a, b) {
|
|
36
|
+
return a <= b;
|
|
93
37
|
}
|
|
94
|
-
|
|
95
|
-
|
|
38
|
+
function gte(a, b) {
|
|
39
|
+
return a >= b;
|
|
96
40
|
}
|
|
97
|
-
|
|
98
|
-
|
|
41
|
+
function gt(a, b) {
|
|
42
|
+
return a > b;
|
|
99
43
|
}
|
|
44
|
+
export {
|
|
45
|
+
OPERATORS,
|
|
46
|
+
eq,
|
|
47
|
+
gt,
|
|
48
|
+
gte,
|
|
49
|
+
lt,
|
|
50
|
+
lte,
|
|
51
|
+
neq,
|
|
52
|
+
numericOp,
|
|
53
|
+
stringOp
|
|
54
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/compare",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.9",
|
|
4
4
|
"description": "Comparators with support for types implementing the @thi.ng/api/ICompare interface",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -33,10 +35,11 @@
|
|
|
33
35
|
"test": "bun test"
|
|
34
36
|
},
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"@thi.ng/api": "^8.9.
|
|
38
|
+
"@thi.ng/api": "^8.9.13"
|
|
37
39
|
},
|
|
38
40
|
"devDependencies": {
|
|
39
41
|
"@microsoft/api-extractor": "^7.38.3",
|
|
42
|
+
"esbuild": "^0.19.8",
|
|
40
43
|
"rimraf": "^5.0.5",
|
|
41
44
|
"tools": "^0.0.1",
|
|
42
45
|
"typedoc": "^0.25.4",
|
|
@@ -51,7 +54,7 @@
|
|
|
51
54
|
"access": "public"
|
|
52
55
|
},
|
|
53
56
|
"engines": {
|
|
54
|
-
"node": ">=
|
|
57
|
+
"node": ">=18"
|
|
55
58
|
},
|
|
56
59
|
"files": [
|
|
57
60
|
"./*.js",
|
|
@@ -77,5 +80,5 @@
|
|
|
77
80
|
"default": "./reverse.js"
|
|
78
81
|
}
|
|
79
82
|
},
|
|
80
|
-
"gitHead": "
|
|
83
|
+
"gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n"
|
|
81
84
|
}
|
package/reverse.js
CHANGED