@thi.ng/compare 2.2.7 → 2.2.8

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:03Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
package/README.md CHANGED
@@ -58,7 +58,7 @@ For Node.js REPL:
58
58
  const compare = await import("@thi.ng/compare");
59
59
  ```
60
60
 
61
- Package sizes (brotli'd, pre-treeshake): ESM: 598 bytes
61
+ Package sizes (brotli'd, pre-treeshake): ESM: 601 bytes
62
62
 
63
63
  ## Dependencies
64
64
 
package/compare.js CHANGED
@@ -1,18 +1,21 @@
1
- export 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;
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
- export function compareByKey(a, cmp = compare) {
4
- const k = getKey(a);
5
- return (x, y) => cmp(k(x), k(y));
3
+ function compareByKey(a, cmp = compare) {
4
+ const k = getKey(a);
5
+ return (x, y) => cmp(k(x), k(y));
6
6
  }
7
- export 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
- };
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
- export 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
22
- ? (res = cmpB(kb(x), kb(y))) === 0
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
- export function compareByKeys4(a, b, c, d, cmpA = compare, cmpB = compare, cmpC = compare, cmpD = compare) {
29
- const ka = getKey(a);
30
- const kb = getKey(b);
31
- const kc = getKey(c);
32
- const kd = getKey(d);
33
- return (x, y) => {
34
- let res = cmpA(ka(x), ka(y));
35
- return res === 0
36
- ? (res = cmpB(kb(x), kb(y))) === 0
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
- * Numeric comparator (ascending order)
3
- *
4
- * @param a -
5
- * @param b -
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
- export const OPERATORS = {
2
- "=": eq,
3
- "!=": neq,
4
- "<": lt,
5
- "<=": lte,
6
- ">=": gte,
7
- ">": gt,
1
+ const OPERATORS = {
2
+ "=": eq,
3
+ "!=": neq,
4
+ "<": lt,
5
+ "<=": lte,
6
+ ">=": gte,
7
+ ">": gt
8
8
  };
9
9
  const __ensure = (op) => {
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;
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
- * Takes a comparison operator (either as string alias or function) and the RHS arg
20
- * for it, returns a new function for given operator which only takes a single
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
- * Similar to {@link stringOp}, but for numeric args. Takes a comparison
55
- * operator (either as string alias or function) and the RHS arg for it, returns
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
- export function eq(a, b) {
83
- return a === b;
26
+ function eq(a, b) {
27
+ return a === b;
84
28
  }
85
- export function neq(a, b) {
86
- return a !== b;
29
+ function neq(a, b) {
30
+ return a !== b;
87
31
  }
88
- export function lt(a, b) {
89
- return a < b;
32
+ function lt(a, b) {
33
+ return a < b;
90
34
  }
91
- export function lte(a, b) {
92
- return a <= b;
35
+ function lte(a, b) {
36
+ return a <= b;
93
37
  }
94
- export function gte(a, b) {
95
- return a >= b;
38
+ function gte(a, b) {
39
+ return a >= b;
96
40
  }
97
- export function gt(a, b) {
98
- return a > b;
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.7",
3
+ "version": "2.2.8",
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 clean && tsc --declaration",
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.11"
38
+ "@thi.ng/api": "^8.9.12"
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",
@@ -77,5 +80,5 @@
77
80
  "default": "./reverse.js"
78
81
  }
79
82
  },
80
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
83
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
81
84
  }
package/reverse.js CHANGED
@@ -1,7 +1,4 @@
1
- /**
2
- * HOF comparator. Returns new comparator with reversed order of given
3
- * comparator.
4
- *
5
- * @param cmp -
6
- */
7
- export const reverse = (cmp) => (a, b) => -cmp(a, b);
1
+ const reverse = (cmp) => (a, b) => -cmp(a, b);
2
+ export {
3
+ reverse
4
+ };