@rickosborne/rebound
Version:
Rick Osborne's utilities for working with bounded numbers
130 lines (129 loc) • 3.81 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { IntegerRange } from "./integer-range.mjs";
import { RealRange } from "./real-range.mjs";
import { INT_SET, LOWER_EX, LOWER_IN, REAL_SET, UPPER_EX, UPPER_IN } from "./spec.mjs";
function checkMissing(config) {
const missing = [];
if (config.lower == null || config.lowerInc == null) {
missing.push("Lower bound");
}
if (config.int == null) {
missing.push("Int/Real");
}
if (config.upper == null && config.upperInc == null) {
missing.push("Upper bound");
}
if (missing.length > 0) {
throw new Error(`Missing configuration: ${missing.join(", ")}`);
}
}
__name(checkMissing, "checkMissing");
const _ReboundBuilder = class _ReboundBuilder {
constructor(config, toRebound) {
this.config = config;
this.toRebound = toRebound;
}
build() {
checkMissing(this.config);
let range;
if (this.config.int === INT_SET) {
range = new IntegerRange(this.config.lower, this.config.upper);
} else {
range = new RealRange(this.config.lowerInc === LOWER_IN, this.config.lower, this.config.upper, this.config.upperInc === UPPER_IN);
}
return this.toRebound(this.config, range);
}
fromExclusive(value) {
this.setLower(value, LOWER_EX);
return this;
}
fromInclusive(value) {
this.setLower(value, LOWER_IN);
return this;
}
fromNegInfinity() {
this.setLower(-Infinity, LOWER_IN);
return this;
}
fromValue(lower, inclusive) {
const lowerInc = inclusive ? LOWER_IN : LOWER_EX;
this.setLower(lower, lowerInc);
return this;
}
intOnly(intOnly) {
const int = intOnly ? INT_SET : REAL_SET;
this.setIntReal(int);
return this;
}
integers() {
this.setIntReal(INT_SET);
return this;
}
reals() {
this.setIntReal(REAL_SET);
return this;
}
setIntReal(int) {
if (int === INT_SET && (this.config.lowerInc != null && this.config.lowerInc === LOWER_EX || this.config.upperInc != null && this.config.upperInc === UPPER_EX)) {
throw new Error("Integer ranges must have inclusive bounds");
}
this.config.int = int;
}
setLower(lower, lowerInc) {
if (Number.isNaN(lower)) {
throw new Error("Bound cannot be NaN");
}
if (lower === Infinity) {
throw new Error("Use -Infinity for a lower bound");
}
if (this.config.upper != null && lower > this.config.upper) {
throw new Error("Bounds are reversed");
}
if (lowerInc === LOWER_EX && this.config.int == INT_SET) {
throw new Error("Integer bounds must be inclusive");
}
this.config.lower = lower;
this.config.lowerInc = lowerInc;
}
setUpper(upper, upperInc) {
if (Number.isNaN(upper)) {
throw new Error("Bound cannot be NaN");
}
if (upper === -Infinity) {
throw new Error("Use Infinity for an upper bound");
}
if (this.config.lower != null && upper < this.config.lower) {
throw new Error("Bounds are reversed");
}
if (upperInc === UPPER_EX && this.config.int == INT_SET) {
throw new Error("Integer bounds must be inclusive");
}
this.config.upper = upper;
this.config.upperInc = upperInc;
}
toExclusive(value) {
this.setUpper(value, UPPER_EX);
return this;
}
toInclusive(value) {
this.setUpper(value, UPPER_IN);
return this;
}
toPosInfinity() {
this.setUpper(Infinity, UPPER_IN);
return this;
}
toValue(value, inclusive) {
const upperInc = inclusive ? UPPER_IN : UPPER_EX;
this.setUpper(value, upperInc);
return this;
}
};
__name(_ReboundBuilder, "ReboundBuilder");
let ReboundBuilder = _ReboundBuilder;
export {
ReboundBuilder,
checkMissing
};
//# sourceMappingURL=rebound-builder.mjs.map