@rickosborne/rebound
Version:
Rick Osborne's utilities for working with bounded numbers
109 lines (108 loc) • 3.57 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { Bound, boundComparator } from "./bound.mjs";
import { unbounded } from "./range-like.mjs";
const _RangeBase = class _RangeBase {
comparator;
isBounded;
isBoundedAbove;
isBoundedBelow;
isEmpty;
isLowerInc;
isSingleton;
isUpperInc;
lowerBound;
lowerEndpoint;
upperBound;
upperEndpoint;
constructor(isLowerInc, lowerBound, upperBound, isUpperInc, comparator) {
if (lowerBound === unbounded && !isLowerInc) {
throw new Error("Lower should be inclusive when unbounded");
}
if (upperBound === unbounded && !isUpperInc) {
throw new Error("Upper should be inclusive when unbounded");
}
this.comparator = comparator;
this.lowerBound = lowerBound === unbounded ? unbounded : isLowerInc ? Bound.gte(lowerBound, comparator) : Bound.gt(lowerBound, comparator);
this.upperBound = upperBound === unbounded ? unbounded : isUpperInc ? Bound.lte(upperBound, comparator) : Bound.lt(upperBound, comparator);
this.isLowerInc = isLowerInc;
this.isUpperInc = isUpperInc;
this.isBoundedAbove = upperBound !== unbounded;
this.isBoundedBelow = lowerBound !== unbounded;
this.isBounded = this.isBoundedAbove && this.isBoundedBelow;
if (lowerBound !== unbounded && upperBound !== unbounded && comparator(lowerBound, upperBound) === 0) {
if (!isLowerInc && !isUpperInc) {
throw new Error("Expected at least one inclusive when lower === upper");
}
this.isSingleton = isLowerInc && isUpperInc;
this.isEmpty = !this.isSingleton;
} else {
this.isSingleton = false;
this.isEmpty = false;
}
this.lowerEndpoint = lowerBound === unbounded ? void 0 : lowerBound;
this.upperEndpoint = upperBound === unbounded ? void 0 : upperBound;
}
assertIsA(obj) {
if (!this.isType(obj)) {
throw new RangeError(`Incorrect type: ${typeof obj}`);
}
if (!this.isA(obj)) {
throw new RangeError(`Out of range: ${String(obj)}`);
}
}
castAs(obj) {
this.assertIsA(obj);
return obj;
}
compareTo(other) {
if (this.lowerBound === unbounded) {
if (other.lowerBound !== unbounded) return -1;
} else if (other.lowerBound === unbounded) {
return 1;
} else {
const lowerCompare = this.lowerBound.compareTo(other.lowerBound);
if (lowerCompare !== 0) {
return lowerCompare;
}
}
if (this.upperBound === unbounded) {
if (other.upperBound !== unbounded) return 1;
} else if (other.upperBound === unbounded) {
return -1;
} else {
const upperCompare = this.upperBound.compareTo(other.upperBound);
if (upperCompare !== 0) {
return upperCompare;
}
}
return 0;
}
contains(value) {
return (this.lowerBound === unbounded || this.lowerBound.isValid(value)) && (this.upperBound === unbounded || this.upperBound.isValid(value));
}
encloses(other) {
if (other === this) {
return true;
}
if (this.comparator !== other.comparator) {
return false;
}
return boundComparator(this.lowerBound, other.lowerBound) <= 0 && boundComparator(this.upperBound, other.upperBound) >= 0;
}
isA(obj) {
return this.isType(obj) && this.contains(obj);
}
toString() {
return this.label;
}
[Symbol.toStringTag]() {
return this.label;
}
};
__name(_RangeBase, "RangeBase");
let RangeBase = _RangeBase;
export {
RangeBase
};
//# sourceMappingURL=range-base.mjs.map