UNPKG

@rickosborne/rebound

Version:

Rick Osborne's utilities for working with bounded numbers

79 lines (78 loc) 2.37 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { numberAsc } from "@rickosborne/foundation"; import { RangeBase } from "./range-base.mjs"; import { unbounded } from "./range-like.mjs"; import { rangeFromChecked } from "./range.mjs"; const _NumberRange = class _NumberRange extends RangeBase { constructor(isLowerInc, lower, isInt, upper, isUpperInc) { if (!Number.isFinite(lower) && !isLowerInc || !Number.isFinite(upper) && !isUpperInc) { throw new RangeError(`Unbounded (infinite) upper/lower must be inclusive`); } let isSingleton = false; let isEmpty = false; if (lower > upper) { throw new RangeError("Expected lower <= upper"); } if (lower === upper) { if (!isLowerInc && !isUpperInc) { throw new Error("Expected at least one inclusive when lower === upper"); } isSingleton = isLowerInc && isUpperInc; isEmpty = !isSingleton; } super( isLowerInc, Number.isFinite(lower) ? lower : unbounded, Number.isFinite(upper) ? upper : unbounded, isUpperInc, numberAsc ); this.lower = lower; this.isInt = isInt; this.upper = upper; this.isSingleton = isSingleton; this.isEmpty = isEmpty; this.label = rangeFromChecked({ isInt, isLowerInc, isUpperInc, lower, upper }); } isEmpty; isSingleton; label; assertCanScaleFrom(other) { if (!this.isBounded || !other.isBounded) { throw new RangeError("Cannot scale with unbounded range"); } if (other.lower >= other.upper) { throw new RangeError("Not enough source range"); } } compareTo(other) { const compared = super.compareTo(other); if (compared !== 0) { return compared; } if (other instanceof _NumberRange) { if (this.isInt && !other.isInt) { return -1; } else if (!this.isInt && other.isInt) { return 1; } } return 0; } encloses(other) { if (other instanceof _NumberRange && this.step > other.step) { return false; } return super.encloses(other); } isType(obj) { return typeof obj === "number" && !Number.isNaN(obj); } }; __name(_NumberRange, "NumberRange"); let NumberRange = _NumberRange; export { NumberRange }; //# sourceMappingURL=number-range.mjs.map