@thi.ng/compare
Advanced tools
Comparing version 2.2.27 to 2.3.0
# Change Log | ||
- **Last updated**: 2024-03-27T09:53:45Z | ||
- **Last updated**: 2024-03-29T12:16:20Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -12,2 +12,8 @@ | ||
## [2.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/compare@2.3.0) (2024-03-29) | ||
#### 🚀 Features | ||
- add compareLengthAsc/Desc(), refactor, add docs ([49dab9e](https://github.com/thi-ng/umbrella/commit/49dab9e)) | ||
## [2.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/compare@2.2.0) (2023-09-15) | ||
@@ -14,0 +20,0 @@ |
@@ -0,2 +1,28 @@ | ||
/** | ||
* Generic comparator (for arbitrary types) with support for | ||
* [ICompare](https://docs.thi.ng/umbrella/api/interfaces/ICompare.html) | ||
* implementations and using the rules described below. Returns standard | ||
* comparator result, i.e. a negative value if `a < b`, a positive value if `a > | ||
* b` and zero otherwise. | ||
* | ||
* @remarks | ||
* Special handling for the following case, in the given order: | ||
* | ||
* `a === b` => 0 | ||
* | ||
* | a | b | result | | ||
* |----------|----------|--------------:| | ||
* | null | null | 0 | | ||
* | null | non-null | -1 | | ||
* | non-null | null | 1 | | ||
* | ICompare | any | a.compare(b) | | ||
* | any | ICompare | -b.compare(a) | | ||
* | ||
* Note: `null` here also includes `undefined` | ||
* | ||
* @param a | ||
* @param b | ||
* @returns | ||
*/ | ||
export declare const compare: (a: any, b: any) => number; | ||
//# sourceMappingURL=compare.d.ts.map |
export * from "./compare.js"; | ||
export * from "./keys.js"; | ||
export * from "./length.js"; | ||
export * from "./numeric.js"; | ||
@@ -4,0 +5,0 @@ export * from "./ops.js"; |
export * from "./compare.js"; | ||
export * from "./keys.js"; | ||
export * from "./length.js"; | ||
export * from "./numeric.js"; | ||
export * from "./ops.js"; | ||
export * from "./reverse.js"; |
22
keys.js
import { compare } from "./compare.js"; | ||
const getKey = (k) => typeof k === "function" ? k : (x) => x[k]; | ||
const __key = (k) => typeof k === "function" ? k : (x) => x[k]; | ||
function compareByKey(key, cmp = compare) { | ||
const kfn = getKey(key); | ||
const kfn = __key(key); | ||
return (x, y) => cmp(kfn(x), kfn(y)); | ||
} | ||
function compareByKeys2(a, b, cmpA = compare, cmpB = compare) { | ||
const ka = getKey(a); | ||
const kb = getKey(b); | ||
const ka = __key(a); | ||
const kb = __key(b); | ||
return (x, y) => { | ||
@@ -16,5 +16,5 @@ let res = cmpA(ka(x), ka(y)); | ||
function compareByKeys3(a, b, c, cmpA = compare, cmpB = compare, cmpC = compare) { | ||
const ka = getKey(a); | ||
const kb = getKey(b); | ||
const kc = getKey(c); | ||
const ka = __key(a); | ||
const kb = __key(b); | ||
const kc = __key(c); | ||
return (x, y) => { | ||
@@ -26,6 +26,6 @@ let res = cmpA(ka(x), ka(y)); | ||
function compareByKeys4(a, b, c, d, cmpA = compare, cmpB = compare, cmpC = compare, cmpD = compare) { | ||
const ka = getKey(a); | ||
const kb = getKey(b); | ||
const kc = getKey(c); | ||
const kd = getKey(d); | ||
const ka = __key(a); | ||
const kb = __key(b); | ||
const kc = __key(c); | ||
const kd = __key(d); | ||
return (x, y) => { | ||
@@ -32,0 +32,0 @@ let res = cmpA(ka(x), ka(y)); |
@@ -1,2 +0,2 @@ | ||
import type { FnN2 } from "@thi.ng/api"; | ||
import type { Comparator } from "@thi.ng/api"; | ||
/** | ||
@@ -8,3 +8,3 @@ * Numeric comparator (ascending order) | ||
*/ | ||
export declare const compareNumAsc: FnN2; | ||
export declare const compareNumAsc: Comparator<number>; | ||
/** | ||
@@ -16,3 +16,3 @@ * Numeric comparator (descending order) | ||
*/ | ||
export declare const compareNumDesc: FnN2; | ||
export declare const compareNumDesc: Comparator<number>; | ||
//# sourceMappingURL=numeric.d.ts.map |
{ | ||
"name": "@thi.ng/compare", | ||
"version": "2.2.27", | ||
"version": "2.3.0", | ||
"description": "Comparators with support for types implementing the @thi.ng/api/ICompare interface", | ||
@@ -73,2 +73,5 @@ "type": "module", | ||
}, | ||
"./length": { | ||
"default": "./length.js" | ||
}, | ||
"./numeric": { | ||
@@ -84,3 +87,3 @@ "default": "./numeric.js" | ||
}, | ||
"gitHead": "feb3b24654f2c931cd3c3308c1c0c807ee14d0e4\n" | ||
"gitHead": "ce5ae2a322d50a7ce8ecccbd94fa55c496ba04fd\n" | ||
} |
@@ -18,2 +18,5 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
- [About](#about) | ||
- [Generic comparison](#generic-comparison) | ||
- [Additional comparators](#additional-comparators) | ||
- [Operators](#operators) | ||
- [Status](#status) | ||
@@ -35,7 +38,29 @@ - [Installation](#installation) | ||
Since v1.2.0 additional higher-order comparators are included, e.g. to | ||
reverse the ordering of an existing comparator and allow hierarchical | ||
sorting by multiple keys/dimensions, each with their own optional | ||
comparator. See examples below. | ||
### Generic comparison | ||
- [`compare()`](https://docs.thi.ng/umbrella/compare/functions/compare.html) | ||
### Additional comparators | ||
- [`compareByKey()`](https://docs.thi.ng/umbrella/compare/functions/compareByKey.html) | ||
- [`compareByKeys2()`](https://docs.thi.ng/umbrella/compare/functions/compareByKeys2.html) | ||
- [`compareByKeys3()`](https://docs.thi.ng/umbrella/compare/functions/compareByKeys3.html) | ||
- [`compareByKeys4()`](https://docs.thi.ng/umbrella/compare/functions/compareByKeys4.html) | ||
- [`compareLengthAsc()`](https://docs.thi.ng/umbrella/compare/functions/compareLengthAsc.html) | ||
- [`compareLengthDesc()`](https://docs.thi.ng/umbrella/compare/functions/compareLengthDesc.html) | ||
- [`compareNumAsc()`](https://docs.thi.ng/umbrella/compare/functions/compareNumAsc.html) | ||
- [`compareNumDesc()`](https://docs.thi.ng/umbrella/compare/functions/compareNumDesc.html) | ||
- [`reverse()`](https://docs.thi.ng/umbrella/compare/functions/reverse.html) | ||
### Operators | ||
- [`numericOp()`](https://docs.thi.ng/umbrella/compare/functions/numericOp.html) | ||
- [`stringOp()`](https://docs.thi.ng/umbrella/compare/functions/stringOp.html) | ||
- [`eq()`](https://docs.thi.ng/umbrella/compare/functions/eq.html) | ||
- [`gt()`](https://docs.thi.ng/umbrella/compare/functions/gt.html) | ||
- [`gte()`](https://docs.thi.ng/umbrella/compare/functions/gte.html) | ||
- [`lt()`](https://docs.thi.ng/umbrella/compare/functions/lt.html) | ||
- [`lte()`](https://docs.thi.ng/umbrella/compare/functions/lte.html) | ||
- [`neq()`](https://docs.thi.ng/umbrella/compare/functions/neq.html) | ||
## Status | ||
@@ -67,3 +92,3 @@ | ||
Package sizes (brotli'd, pre-treeshake): ESM: 601 bytes | ||
Package sizes (brotli'd, pre-treeshake): ESM: 629 bytes | ||
@@ -117,3 +142,3 @@ ## Dependencies | ||
```ts | ||
```ts tangle:export/readme1.ts | ||
import * as cmp from "@thi.ng/compare"; | ||
@@ -129,3 +154,5 @@ | ||
// cluster sort by id -> age (default comparators) | ||
[...src].sort(cmp.compareByKeys2("id", "age")); | ||
console.log( | ||
[...src].sort(cmp.compareByKeys2("id", "age")) | ||
); | ||
// [ | ||
@@ -139,3 +166,5 @@ // { id: 'alice', age: 23 }, | ||
// cluster sort by age -> id (default comparators) | ||
[...src].sort(cmp.compareByKeys2("age", "id")); | ||
console.log( | ||
[...src].sort(cmp.compareByKeys2("age", "id")) | ||
); | ||
// [ | ||
@@ -150,3 +179,5 @@ // { id: 'dora', age: 11 }, | ||
// (custom comparator for `age` key) | ||
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compareNumDesc)); | ||
console.log( | ||
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compareNumDesc)) | ||
); | ||
// [ | ||
@@ -160,3 +191,5 @@ // { id: 'charlie', age: 66 }, | ||
// using `reverse()` comparator for `id` | ||
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compare, cmp.reverse(cmp.compare))); | ||
console.log( | ||
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compare, cmp.reverse(cmp.compare))) | ||
); | ||
// [ | ||
@@ -163,0 +196,0 @@ // { id: 'dora', age: 11 }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36442
18
332
213