@thi.ng/compare 2.2.27 → 2.3.1

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**: 2024-03-27T09:53:45Z
3
+ - **Last updated**: 2024-04-08T14:59:29Z
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.
@@ -9,6 +9,12 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [2.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/compare@2.3.0) (2024-03-29)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add compareLengthAsc/Desc(), refactor, add docs ([49dab9e](https://github.com/thi-ng/umbrella/commit/49dab9e))
17
+
12
18
  ## [2.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/compare@2.2.0) (2023-09-15)
13
19
 
14
20
  #### 🚀 Features
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 190 standalone projects, maintained as part
10
+ > This is one of 191 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -15,6 +15,9 @@
15
15
  > GitHub](https://github.com/sponsors/postspectacular). Thank you! ❤️
16
16
 
17
17
  - [About](#about)
18
+ - [Generic comparison](#generic-comparison)
19
+ - [Additional comparators](#additional-comparators)
20
+ - [Operators](#operators)
18
21
  - [Status](#status)
19
22
  - [Installation](#installation)
20
23
  - [Dependencies](#dependencies)
@@ -31,10 +34,32 @@ Comparators with optional support for types implementing the [@thi.ng/api
31
34
  `ICompare`](https://github.com/thi-ng/umbrella/tree/develop/packages/api/src/compare.ts)
32
35
  interface.
33
36
 
34
- Since v1.2.0 additional higher-order comparators are included, e.g. to
35
- reverse the ordering of an existing comparator and allow hierarchical
36
- sorting by multiple keys/dimensions, each with their own optional
37
- comparator. See examples below.
37
+ ### Generic comparison
38
+
39
+ - [`compare()`](https://docs.thi.ng/umbrella/compare/functions/compare.html)
40
+
41
+ ### Additional comparators
42
+
43
+ - [`compareByKey()`](https://docs.thi.ng/umbrella/compare/functions/compareByKey.html)
44
+ - [`compareByKeys2()`](https://docs.thi.ng/umbrella/compare/functions/compareByKeys2.html)
45
+ - [`compareByKeys3()`](https://docs.thi.ng/umbrella/compare/functions/compareByKeys3.html)
46
+ - [`compareByKeys4()`](https://docs.thi.ng/umbrella/compare/functions/compareByKeys4.html)
47
+ - [`compareLengthAsc()`](https://docs.thi.ng/umbrella/compare/functions/compareLengthAsc.html)
48
+ - [`compareLengthDesc()`](https://docs.thi.ng/umbrella/compare/functions/compareLengthDesc.html)
49
+ - [`compareNumAsc()`](https://docs.thi.ng/umbrella/compare/functions/compareNumAsc.html)
50
+ - [`compareNumDesc()`](https://docs.thi.ng/umbrella/compare/functions/compareNumDesc.html)
51
+ - [`reverse()`](https://docs.thi.ng/umbrella/compare/functions/reverse.html)
52
+
53
+ ### Operators
54
+
55
+ - [`numericOp()`](https://docs.thi.ng/umbrella/compare/functions/numericOp.html)
56
+ - [`stringOp()`](https://docs.thi.ng/umbrella/compare/functions/stringOp.html)
57
+ - [`eq()`](https://docs.thi.ng/umbrella/compare/functions/eq.html)
58
+ - [`gt()`](https://docs.thi.ng/umbrella/compare/functions/gt.html)
59
+ - [`gte()`](https://docs.thi.ng/umbrella/compare/functions/gte.html)
60
+ - [`lt()`](https://docs.thi.ng/umbrella/compare/functions/lt.html)
61
+ - [`lte()`](https://docs.thi.ng/umbrella/compare/functions/lte.html)
62
+ - [`neq()`](https://docs.thi.ng/umbrella/compare/functions/neq.html)
38
63
 
39
64
  ## Status
40
65
 
@@ -48,7 +73,13 @@ comparator. See examples below.
48
73
  yarn add @thi.ng/compare
49
74
  ```
50
75
 
51
- ES module import:
76
+ ESM import:
77
+
78
+ ```ts
79
+ import * as cmp from "@thi.ng/compare";
80
+ ```
81
+
82
+ Browser ESM import:
52
83
 
53
84
  ```html
54
85
  <script type="module" src="https://cdn.skypack.dev/@thi.ng/compare"></script>
@@ -59,10 +90,10 @@ ES module import:
59
90
  For Node.js REPL:
60
91
 
61
92
  ```js
62
- const compare = await import("@thi.ng/compare");
93
+ const cmp = await import("@thi.ng/compare");
63
94
  ```
64
95
 
65
- Package sizes (brotli'd, pre-treeshake): ESM: 601 bytes
96
+ Package sizes (brotli'd, pre-treeshake): ESM: 629 bytes
66
97
 
67
98
  ## Dependencies
68
99
 
@@ -111,7 +142,7 @@ compare(new Foo(1), new Foo(2));
111
142
 
112
143
  Key-based object comparison is supported for 1 - 4 keys / dimensions.
113
144
 
114
- ```ts
145
+ ```ts tangle:export/readme1.ts
115
146
  import * as cmp from "@thi.ng/compare";
116
147
 
117
148
  const src = [
@@ -122,7 +153,9 @@ const src = [
122
153
  ];
123
154
 
124
155
  // cluster sort by id -> age (default comparators)
125
- [...src].sort(cmp.compareByKeys2("id", "age"));
156
+ console.log(
157
+ [...src].sort(cmp.compareByKeys2("id", "age"))
158
+ );
126
159
  // [
127
160
  // { id: 'alice', age: 23 },
128
161
  // { id: 'bart', age: 42 },
@@ -131,7 +164,9 @@ const src = [
131
164
  // ]
132
165
 
133
166
  // cluster sort by age -> id (default comparators)
134
- [...src].sort(cmp.compareByKeys2("age", "id"));
167
+ console.log(
168
+ [...src].sort(cmp.compareByKeys2("age", "id"))
169
+ );
135
170
  // [
136
171
  // { id: 'dora', age: 11 },
137
172
  // { id: 'alice', age: 23 },
@@ -141,7 +176,9 @@ const src = [
141
176
 
142
177
  // cluster sort by age -> id
143
178
  // (custom comparator for `age` key)
144
- [...src].sort(cmp.compareByKeys2("age", "id", cmp.compareNumDesc));
179
+ console.log(
180
+ [...src].sort(cmp.compareByKeys2("age", "id", cmp.compareNumDesc))
181
+ );
145
182
  // [
146
183
  // { id: 'charlie', age: 66 },
147
184
  // { id: 'bart', age: 42 },
@@ -150,7 +187,9 @@ const src = [
150
187
  // ]
151
188
 
152
189
  // using `reverse()` comparator for `id`
153
- [...src].sort(cmp.compareByKeys2("age", "id", cmp.compare, cmp.reverse(cmp.compare)));
190
+ console.log(
191
+ [...src].sort(cmp.compareByKeys2("age", "id", cmp.compare, cmp.reverse(cmp.compare)))
192
+ );
154
193
  // [
155
194
  // { id: 'dora', age: 11 },
156
195
  // { id: 'alice', age: 23 },
package/compare.d.ts CHANGED
@@ -1,2 +1,28 @@
1
+ /**
2
+ * Generic comparator (for arbitrary types) with support for
3
+ * [ICompare](https://docs.thi.ng/umbrella/api/interfaces/ICompare.html)
4
+ * implementations and using the rules described below. Returns standard
5
+ * comparator result, i.e. a negative value if `a < b`, a positive value if `a >
6
+ * b` and zero otherwise.
7
+ *
8
+ * @remarks
9
+ * Special handling for the following case, in the given order:
10
+ *
11
+ * `a === b` => 0
12
+ *
13
+ * | a | b | result |
14
+ * |----------|----------|--------------:|
15
+ * | null | null | 0 |
16
+ * | null | non-null | -1 |
17
+ * | non-null | null | 1 |
18
+ * | ICompare | any | a.compare(b) |
19
+ * | any | ICompare | -b.compare(a) |
20
+ *
21
+ * Note: `null` here also includes `undefined`
22
+ *
23
+ * @param a
24
+ * @param b
25
+ * @returns
26
+ */
1
27
  export declare const compare: (a: any, b: any) => number;
2
28
  //# sourceMappingURL=compare.d.ts.map
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./compare.js";
2
2
  export * from "./keys.js";
3
+ export * from "./length.js";
3
4
  export * from "./numeric.js";
4
5
  export * from "./ops.js";
5
6
  export * from "./reverse.js";
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./compare.js";
2
2
  export * from "./keys.js";
3
+ export * from "./length.js";
3
4
  export * from "./numeric.js";
4
5
  export * from "./ops.js";
5
6
  export * from "./reverse.js";
package/keys.js CHANGED
@@ -1,31 +1,31 @@
1
1
  import { compare } from "./compare.js";
2
- const getKey = (k) => typeof k === "function" ? k : (x) => x[k];
2
+ const __key = (k) => typeof k === "function" ? k : (x) => x[k];
3
3
  function compareByKey(key, cmp = compare) {
4
- const kfn = getKey(key);
4
+ const kfn = __key(key);
5
5
  return (x, y) => cmp(kfn(x), kfn(y));
6
6
  }
7
7
  function compareByKeys2(a, b, cmpA = compare, cmpB = compare) {
8
- const ka = getKey(a);
9
- const kb = getKey(b);
8
+ const ka = __key(a);
9
+ const kb = __key(b);
10
10
  return (x, y) => {
11
11
  let res = cmpA(ka(x), ka(y));
12
12
  return res === 0 ? cmpB(kb(x), kb(y)) : res;
13
13
  };
14
14
  }
15
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);
16
+ const ka = __key(a);
17
+ const kb = __key(b);
18
+ const kc = __key(c);
19
19
  return (x, y) => {
20
20
  let res = cmpA(ka(x), ka(y));
21
21
  return res === 0 ? (res = cmpB(kb(x), kb(y))) === 0 ? cmpC(kc(x), kc(y)) : res : res;
22
22
  };
23
23
  }
24
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);
25
+ const ka = __key(a);
26
+ const kb = __key(b);
27
+ const kc = __key(c);
28
+ const kd = __key(d);
29
29
  return (x, y) => {
30
30
  let res = cmpA(ka(x), ka(y));
31
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;
package/length.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { Comparator, ILength } from "@thi.ng/api";
2
+ /**
3
+ * Length comparator (ascending order)
4
+ *
5
+ * @param a -
6
+ * @param b -
7
+ */
8
+ export declare const compareLengthAsc: Comparator<ILength>;
9
+ /**
10
+ * Length comparator (descending order)
11
+ *
12
+ * @param a -
13
+ * @param b -
14
+ */
15
+ export declare const compareLengthDesc: Comparator<ILength>;
16
+ //# sourceMappingURL=length.d.ts.map
package/length.js ADDED
@@ -0,0 +1,6 @@
1
+ const compareLengthAsc = (a, b) => a.length - b.length;
2
+ const compareLengthDesc = (a, b) => b.length - a.length;
3
+ export {
4
+ compareLengthAsc,
5
+ compareLengthDesc
6
+ };
package/numeric.d.ts CHANGED
@@ -1,16 +1,16 @@
1
- import type { FnN2 } from "@thi.ng/api";
1
+ import type { Comparator } from "@thi.ng/api";
2
2
  /**
3
3
  * Numeric comparator (ascending order)
4
4
  *
5
5
  * @param a -
6
6
  * @param b -
7
7
  */
8
- export declare const compareNumAsc: FnN2;
8
+ export declare const compareNumAsc: Comparator<number>;
9
9
  /**
10
10
  * Numeric comparator (descending order)
11
11
  *
12
12
  * @param a -
13
13
  * @param b -
14
14
  */
15
- export declare const compareNumDesc: FnN2;
15
+ export declare const compareNumDesc: Comparator<number>;
16
16
  //# sourceMappingURL=numeric.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/compare",
3
- "version": "2.2.27",
3
+ "version": "2.3.1",
4
4
  "description": "Comparators with support for types implementing the @thi.ng/api/ICompare interface",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -36,7 +36,7 @@
36
36
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
37
37
  },
38
38
  "dependencies": {
39
- "@thi.ng/api": "^8.9.31"
39
+ "@thi.ng/api": "^8.10.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@microsoft/api-extractor": "^7.43.0",
@@ -70,6 +70,9 @@
70
70
  "./keys": {
71
71
  "default": "./keys.js"
72
72
  },
73
+ "./length": {
74
+ "default": "./length.js"
75
+ },
73
76
  "./numeric": {
74
77
  "default": "./numeric.js"
75
78
  },
@@ -80,5 +83,8 @@
80
83
  "default": "./reverse.js"
81
84
  }
82
85
  },
83
- "gitHead": "feb3b24654f2c931cd3c3308c1c0c807ee14d0e4\n"
86
+ "thi.ng": {
87
+ "alias": "cmp"
88
+ },
89
+ "gitHead": "85ac4bd4d6d89f8e3689e2863d5bea0cecdb371c\n"
84
90
  }