UNPKG

@rickosborne/rebound

Version:

Rick Osborne's utilities for working with bounded numbers

133 lines (132 loc) 4.77 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var bound_exports = {}; __export(bound_exports, { BOUND_GT: () => BOUND_GT, BOUND_GTE: () => BOUND_GTE, BOUND_LT: () => BOUND_LT, BOUND_LTE: () => BOUND_LTE, BOUND_TYPES: () => BOUND_TYPES, Bound: () => Bound, boundComparator: () => boundComparator, boundTypeComparator: () => boundTypeComparator, boundTypeComparisonIsValid: () => boundTypeComparisonIsValid }); module.exports = __toCommonJS(bound_exports); var import_foundation = require("@rickosborne/foundation"); var import_typical = require("@rickosborne/typical"); var import_range_like = require("./range-like.cjs"); 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 === import_range_like.unbounded) { if (b.boundType === BOUND_GT || b.boundType === BOUND_GTE) { return import_typical.A_LT_B; } return import_typical.A_GT_B; } if (b === import_range_like.unbounded) { if (a.boundType === BOUND_GT || a.boundType === BOUND_GTE) { return import_typical.A_GT_B; } return import_typical.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" ? import_foundation.numberAsc : typeof value === "string" ? import_foundation.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" ? import_foundation.numberAsc : typeof value === "string" ? import_foundation.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" ? import_foundation.numberAsc : typeof value === "string" ? import_foundation.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" ? import_foundation.numberAsc : typeof value === "string" ? import_foundation.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; //# sourceMappingURL=bound.cjs.map