@thi.ng/compare
Advanced tools
Comparing version 2.2.7 to 2.2.8
# Change Log | ||
- **Last updated**: 2023-12-09T19:12:03Z | ||
- **Last updated**: 2023-12-11T10:07:09Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -5,0 +5,0 @@ |
@@ -1,18 +0,21 @@ | ||
export const compare = (a, b) => { | ||
if (a === b) { | ||
return 0; | ||
} | ||
if (a == null) { | ||
return b == null ? 0 : -1; | ||
} | ||
if (b == null) { | ||
return a == null ? 0 : 1; | ||
} | ||
if (typeof a.compare === "function") { | ||
return a.compare(b); | ||
} | ||
if (typeof b.compare === "function") { | ||
return -b.compare(a); | ||
} | ||
return a < b ? -1 : a > b ? 1 : 0; | ||
const compare = (a, b) => { | ||
if (a === b) { | ||
return 0; | ||
} | ||
if (a == null) { | ||
return b == null ? 0 : -1; | ||
} | ||
if (b == null) { | ||
return a == null ? 0 : 1; | ||
} | ||
if (typeof a.compare === "function") { | ||
return a.compare(b); | ||
} | ||
if (typeof b.compare === "function") { | ||
return -b.compare(a); | ||
} | ||
return a < b ? -1 : a > b ? 1 : 0; | ||
}; | ||
export { | ||
compare | ||
}; |
70
keys.js
import { compare } from "./compare.js"; | ||
const getKey = (k) => typeof k === "function" ? k : (x) => x[k]; | ||
export function compareByKey(a, cmp = compare) { | ||
const k = getKey(a); | ||
return (x, y) => cmp(k(x), k(y)); | ||
function compareByKey(a, cmp = compare) { | ||
const k = getKey(a); | ||
return (x, y) => cmp(k(x), k(y)); | ||
} | ||
export function compareByKeys2(a, b, cmpA = compare, cmpB = compare) { | ||
const ka = getKey(a); | ||
const kb = getKey(b); | ||
return (x, y) => { | ||
let res = cmpA(ka(x), ka(y)); | ||
return res === 0 ? cmpB(kb(x), kb(y)) : res; | ||
}; | ||
function compareByKeys2(a, b, cmpA = compare, cmpB = compare) { | ||
const ka = getKey(a); | ||
const kb = getKey(b); | ||
return (x, y) => { | ||
let res = cmpA(ka(x), ka(y)); | ||
return res === 0 ? cmpB(kb(x), kb(y)) : res; | ||
}; | ||
} | ||
export function compareByKeys3(a, b, c, cmpA = compare, cmpB = compare, cmpC = compare) { | ||
const ka = getKey(a); | ||
const kb = getKey(b); | ||
const kc = getKey(c); | ||
return (x, y) => { | ||
let res = cmpA(ka(x), ka(y)); | ||
return res === 0 | ||
? (res = cmpB(kb(x), kb(y))) === 0 | ||
? cmpC(kc(x), kc(y)) | ||
: res | ||
: res; | ||
}; | ||
function compareByKeys3(a, b, c, cmpA = compare, cmpB = compare, cmpC = compare) { | ||
const ka = getKey(a); | ||
const kb = getKey(b); | ||
const kc = getKey(c); | ||
return (x, y) => { | ||
let res = cmpA(ka(x), ka(y)); | ||
return res === 0 ? (res = cmpB(kb(x), kb(y))) === 0 ? cmpC(kc(x), kc(y)) : res : res; | ||
}; | ||
} | ||
export 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); | ||
return (x, y) => { | ||
let res = cmpA(ka(x), ka(y)); | ||
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; | ||
}; | ||
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); | ||
return (x, y) => { | ||
let res = cmpA(ka(x), ka(y)); | ||
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; | ||
}; | ||
} | ||
export { | ||
compareByKey, | ||
compareByKeys2, | ||
compareByKeys3, | ||
compareByKeys4 | ||
}; |
@@ -1,14 +0,6 @@ | ||
/** | ||
* Numeric comparator (ascending order) | ||
* | ||
* @param a - | ||
* @param b - | ||
*/ | ||
export const compareNumAsc = (a, b) => a - b; | ||
/** | ||
* Numeric comparator (descending order) | ||
* | ||
* @param a - | ||
* @param b - | ||
*/ | ||
export const compareNumDesc = (a, b) => b - a; | ||
const compareNumAsc = (a, b) => a - b; | ||
const compareNumDesc = (a, b) => b - a; | ||
export { | ||
compareNumAsc, | ||
compareNumDesc | ||
}; |
131
ops.js
@@ -1,99 +0,54 @@ | ||
export const OPERATORS = { | ||
"=": eq, | ||
"!=": neq, | ||
"<": lt, | ||
"<=": lte, | ||
">=": gte, | ||
">": gt, | ||
const OPERATORS = { | ||
"=": eq, | ||
"!=": neq, | ||
"<": lt, | ||
"<=": lte, | ||
">=": gte, | ||
">": gt | ||
}; | ||
const __ensure = (op) => { | ||
if (typeof op === "string") { | ||
if (op in OPERATORS) | ||
return OPERATORS[op]; | ||
else | ||
throw new Error(`invalid operator: ${op}`); | ||
} | ||
return op; | ||
if (typeof op === "string") { | ||
if (op in OPERATORS) | ||
return OPERATORS[op]; | ||
else | ||
throw new Error(`invalid operator: ${op}`); | ||
} | ||
return op; | ||
}; | ||
/** | ||
* Takes a comparison operator (either as string alias or function) and the RHS arg | ||
* for it, returns a new function for given operator which only takes a single | ||
* arg (i.e. the LHS of the operator) and always coerces it to a string before | ||
* applying the operator. | ||
* | ||
* @remarks | ||
* See {@link numericOp} for related functionality. | ||
* | ||
* @example | ||
* ```ts | ||
* const equalsABC = stringOp("=", "abc"); | ||
* | ||
* ["xyz", "abc", "def"].map(equalsABC) | ||
* // [ false, true, false ] | ||
* | ||
* class X { | ||
* constructor(public body: string) {} | ||
* | ||
* toString() { | ||
* return this.body; | ||
* } | ||
* } | ||
* | ||
* equalsABC(new X("abc")) | ||
* // true | ||
* ``` | ||
* | ||
* @param op | ||
* @param x | ||
*/ | ||
export const stringOp = (op, x) => { | ||
const impl = __ensure(op); | ||
return (y) => impl(String(y), x); | ||
const stringOp = (op, x) => { | ||
const impl = __ensure(op); | ||
return (y) => impl(String(y), x); | ||
}; | ||
/** | ||
* Similar to {@link stringOp}, but for numeric args. Takes a comparison | ||
* operator (either as string alias or function) and the RHS arg for it, returns | ||
* a new function for given operator which only takes a single arg (i.e. the LHS | ||
* of the operator) and then checks if that arg is a number before applying the | ||
* operator. For non-numeric args, the returned function will always return | ||
* false. | ||
* | ||
* @example | ||
* ```ts | ||
* const lessThan42 = numericOp("<", 42); | ||
* | ||
* lessThan42(41) | ||
* // true | ||
* | ||
* lessThan42("41") | ||
* // false | ||
* | ||
* lessThan42([41]) | ||
* // false | ||
* ``` | ||
* | ||
* @param op | ||
* @param x | ||
*/ | ||
export const numericOp = (op, x) => { | ||
const impl = __ensure(op); | ||
return (y) => typeof y === "number" && impl(y, x); | ||
const numericOp = (op, x) => { | ||
const impl = __ensure(op); | ||
return (y) => typeof y === "number" && impl(y, x); | ||
}; | ||
export function eq(a, b) { | ||
return a === b; | ||
function eq(a, b) { | ||
return a === b; | ||
} | ||
export function neq(a, b) { | ||
return a !== b; | ||
function neq(a, b) { | ||
return a !== b; | ||
} | ||
export function lt(a, b) { | ||
return a < b; | ||
function lt(a, b) { | ||
return a < b; | ||
} | ||
export function lte(a, b) { | ||
return a <= b; | ||
function lte(a, b) { | ||
return a <= b; | ||
} | ||
export function gte(a, b) { | ||
return a >= b; | ||
function gte(a, b) { | ||
return a >= b; | ||
} | ||
export function gt(a, b) { | ||
return a > b; | ||
function gt(a, b) { | ||
return a > b; | ||
} | ||
export { | ||
OPERATORS, | ||
eq, | ||
gt, | ||
gte, | ||
lt, | ||
lte, | ||
neq, | ||
numericOp, | ||
stringOp | ||
}; |
{ | ||
"name": "@thi.ng/compare", | ||
"version": "2.2.7", | ||
"version": "2.2.8", | ||
"description": "Comparators with support for types implementing the @thi.ng/api/ICompare interface", | ||
@@ -27,3 +27,5 @@ "type": "module", | ||
"scripts": { | ||
"build": "yarn clean && tsc --declaration", | ||
"build": "yarn build:esbuild && yarn build:decl", | ||
"build:decl": "tsc --declaration --emitDeclarationOnly", | ||
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts", | ||
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc", | ||
@@ -37,6 +39,7 @@ "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts", | ||
"dependencies": { | ||
"@thi.ng/api": "^8.9.11" | ||
"@thi.ng/api": "^8.9.12" | ||
}, | ||
"devDependencies": { | ||
"@microsoft/api-extractor": "^7.38.3", | ||
"esbuild": "^0.19.8", | ||
"rimraf": "^5.0.5", | ||
@@ -82,3 +85,3 @@ "tools": "^0.0.1", | ||
}, | ||
"gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n" | ||
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n" | ||
} |
@@ -61,3 +61,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (brotli'd, pre-treeshake): ESM: 598 bytes | ||
Package sizes (brotli'd, pre-treeshake): ESM: 601 bytes | ||
@@ -64,0 +64,0 @@ ## Dependencies |
@@ -1,7 +0,4 @@ | ||
/** | ||
* HOF comparator. Returns new comparator with reversed order of given | ||
* comparator. | ||
* | ||
* @param cmp - | ||
*/ | ||
export const reverse = (cmp) => (a, b) => -cmp(a, b); | ||
const reverse = (cmp) => (a, b) => -cmp(a, b); | ||
export { | ||
reverse | ||
}; |
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
32777
6
279
+ Added@thi.ng/api@8.11.12(transitive)
- Removed@thi.ng/api@8.11.11(transitive)
Updated@thi.ng/api@^8.9.12