@rickosborne/rebound
Version:
Rick Osborne's utilities for working with bounded numbers
114 lines (113 loc) • 3.49 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { numberAsc, stringAsc } from "@rickosborne/foundation";
import { A_GT_B, A_LT_B } from "@rickosborne/typical";
import { unbounded } from "./range-like.mjs";
const BOUND_GT = ">";
const BOUND_GTE = ">=";
const BOUND_LT = "<";
const BOUND_LTE = "<=";
const BOUND_TYPES = Object.freeze([BOUND_LT, BOUND_LTE, BOUND_GTE, BOUND_GT]);
const boundTypeComparator = /* @__PURE__ */ __name((a, b) => {
if (a === b) {
return 0;
}
return BOUND_TYPES.indexOf(a) - BOUND_TYPES.indexOf(b);
}, "boundTypeComparator");
const boundTypeComparisonIsValid = Object.freeze({
[BOUND_GT]: (c) => c > 0,
[BOUND_GTE]: (c) => c >= 0,
[BOUND_LT]: (c) => c < 0,
[BOUND_LTE]: (c) => c <= 0
});
const boundComparator = /* @__PURE__ */ __name((a, b) => {
if (a === b) {
return 0;
}
if (a === unbounded) {
if (b.boundType === BOUND_GT || b.boundType === BOUND_GTE) {
return A_LT_B;
}
return A_GT_B;
}
if (b === unbounded) {
if (a.boundType === BOUND_GT || a.boundType === BOUND_GTE) {
return A_GT_B;
}
return A_LT_B;
}
if (a.comparator !== b.comparator) {
throw new Error(`Mismatched comparators: ${a.comparator.name} ${b.comparator.name}`);
}
const valueComparison = a.comparator(a.value, b.value);
if (valueComparison !== 0) {
return valueComparison;
}
return boundTypeComparator(a.boundType, b.boundType);
}, "boundComparator");
const _Bound = class _Bound {
static gt(value, comparator) {
const cmp = comparator ?? (typeof value === "number" ? numberAsc : typeof value === "string" ? stringAsc : void 0);
if (cmp == null) {
throw new Error("Comparator required");
}
return new _Bound(value, BOUND_GT, cmp);
}
static gte(value, comparator) {
const cmp = comparator ?? (typeof value === "number" ? numberAsc : typeof value === "string" ? stringAsc : void 0);
if (cmp == null) {
throw new Error("Comparator required");
}
return new _Bound(value, BOUND_GTE, cmp);
}
static lt(value, comparator) {
const cmp = comparator ?? (typeof value === "number" ? numberAsc : typeof value === "string" ? stringAsc : void 0);
if (cmp == null) {
throw new Error("Comparator required");
}
return new _Bound(value, BOUND_LT, cmp);
}
static lte(value, comparator) {
const cmp = comparator ?? (typeof value === "number" ? numberAsc : typeof value === "string" ? stringAsc : void 0);
if (cmp == null) {
throw new Error("Comparator required");
}
return new _Bound(value, BOUND_LTE, cmp);
}
boundType;
comparator;
isInc;
validator;
value;
constructor(value, boundType, comparator) {
this.value = value;
this.boundType = boundType;
this.comparator = comparator;
this.isInc = boundType === BOUND_LTE || boundType === BOUND_GTE;
this.validator = boundTypeComparisonIsValid[boundType];
}
compareTo(other) {
return boundComparator(this, other);
}
isValid(value) {
const comparison = this.comparator(value, this.value);
return this.validator(comparison);
}
toString() {
return this.boundType.concat(String(this.value));
}
};
__name(_Bound, "Bound");
let Bound = _Bound;
export {
BOUND_GT,
BOUND_GTE,
BOUND_LT,
BOUND_LTE,
BOUND_TYPES,
Bound,
boundComparator,
boundTypeComparator,
boundTypeComparisonIsValid
};
//# sourceMappingURL=bound.mjs.map