@thi.ng/compare 2.2.27 → 2.3.0
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 +43 -10
- 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 +5 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-03-
|
|
3
|
+
- **Last updated**: 2024-03-29T12:16:20Z
|
|
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
|
@@ -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
|
|
|
@@ -62,7 +87,7 @@ For Node.js REPL:
|
|
|
62
87
|
const compare = await import("@thi.ng/compare");
|
|
63
88
|
```
|
|
64
89
|
|
|
65
|
-
Package sizes (brotli'd, pre-treeshake): ESM:
|
|
90
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 629 bytes
|
|
66
91
|
|
|
67
92
|
## Dependencies
|
|
68
93
|
|
|
@@ -111,7 +136,7 @@ compare(new Foo(1), new Foo(2));
|
|
|
111
136
|
|
|
112
137
|
Key-based object comparison is supported for 1 - 4 keys / dimensions.
|
|
113
138
|
|
|
114
|
-
```ts
|
|
139
|
+
```ts tangle:export/readme1.ts
|
|
115
140
|
import * as cmp from "@thi.ng/compare";
|
|
116
141
|
|
|
117
142
|
const src = [
|
|
@@ -122,7 +147,9 @@ const src = [
|
|
|
122
147
|
];
|
|
123
148
|
|
|
124
149
|
// cluster sort by id -> age (default comparators)
|
|
125
|
-
|
|
150
|
+
console.log(
|
|
151
|
+
[...src].sort(cmp.compareByKeys2("id", "age"))
|
|
152
|
+
);
|
|
126
153
|
// [
|
|
127
154
|
// { id: 'alice', age: 23 },
|
|
128
155
|
// { id: 'bart', age: 42 },
|
|
@@ -131,7 +158,9 @@ const src = [
|
|
|
131
158
|
// ]
|
|
132
159
|
|
|
133
160
|
// cluster sort by age -> id (default comparators)
|
|
134
|
-
|
|
161
|
+
console.log(
|
|
162
|
+
[...src].sort(cmp.compareByKeys2("age", "id"))
|
|
163
|
+
);
|
|
135
164
|
// [
|
|
136
165
|
// { id: 'dora', age: 11 },
|
|
137
166
|
// { id: 'alice', age: 23 },
|
|
@@ -141,7 +170,9 @@ const src = [
|
|
|
141
170
|
|
|
142
171
|
// cluster sort by age -> id
|
|
143
172
|
// (custom comparator for `age` key)
|
|
144
|
-
|
|
173
|
+
console.log(
|
|
174
|
+
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compareNumDesc))
|
|
175
|
+
);
|
|
145
176
|
// [
|
|
146
177
|
// { id: 'charlie', age: 66 },
|
|
147
178
|
// { id: 'bart', age: 42 },
|
|
@@ -150,7 +181,9 @@ const src = [
|
|
|
150
181
|
// ]
|
|
151
182
|
|
|
152
183
|
// using `reverse()` comparator for `id`
|
|
153
|
-
|
|
184
|
+
console.log(
|
|
185
|
+
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compare, cmp.reverse(cmp.compare)))
|
|
186
|
+
);
|
|
154
187
|
// [
|
|
155
188
|
// { id: 'dora', age: 11 },
|
|
156
189
|
// { 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.0",
|
|
4
4
|
"description": "Comparators with support for types implementing the @thi.ng/api/ICompare interface",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -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,5 @@
|
|
|
80
83
|
"default": "./reverse.js"
|
|
81
84
|
}
|
|
82
85
|
},
|
|
83
|
-
"gitHead": "
|
|
86
|
+
"gitHead": "ce5ae2a322d50a7ce8ecccbd94fa55c496ba04fd\n"
|
|
84
87
|
}
|