@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 +7 -1
- package/README.md +52 -13
- package/compare.d.ts +26 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/keys.js +11 -11
- package/length.d.ts +16 -0
- package/length.js +6 -0
- package/numeric.d.ts +3 -3
- package/package.json +9 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-
|
|
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
|
[](https://mastodon.thi.ng/@toxi)
|
|
8
8
|
|
|
9
9
|
> [!NOTE]
|
|
10
|
-
> This is one of
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
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
|
|
93
|
+
const cmp = await import("@thi.ng/compare");
|
|
63
94
|
```
|
|
64
95
|
|
|
65
|
-
Package sizes (brotli'd, pre-treeshake): ESM:
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
package/index.js
CHANGED
package/keys.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
import { compare } from "./compare.js";
|
|
2
|
-
const
|
|
2
|
+
const __key = (k) => typeof k === "function" ? k : (x) => x[k];
|
|
3
3
|
function compareByKey(key, cmp = compare) {
|
|
4
|
-
const kfn =
|
|
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 =
|
|
9
|
-
const kb =
|
|
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 =
|
|
17
|
-
const kb =
|
|
18
|
-
const kc =
|
|
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 =
|
|
26
|
-
const kb =
|
|
27
|
-
const kc =
|
|
28
|
-
const kd =
|
|
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
package/numeric.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import type {
|
|
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:
|
|
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:
|
|
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.
|
|
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.
|
|
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
|
-
"
|
|
86
|
+
"thi.ng": {
|
|
87
|
+
"alias": "cmp"
|
|
88
|
+
},
|
|
89
|
+
"gitHead": "85ac4bd4d6d89f8e3689e2863d5bea0cecdb371c\n"
|
|
84
90
|
}
|