UNPKG

@rickosborne/rebound

Version:

Rick Osborne's utilities for working with bounded numbers

149 lines (148 loc) 5.03 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 rebound_builder_exports = {}; __export(rebound_builder_exports, { ReboundBuilder: () => ReboundBuilder, checkMissing: () => checkMissing }); module.exports = __toCommonJS(rebound_builder_exports); var import_integer_range = require("./integer-range.cjs"); var import_real_range = require("./real-range.cjs"); var import_spec = require("./spec.cjs"); 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 === import_spec.INT_SET) { range = new import_integer_range.IntegerRange(this.config.lower, this.config.upper); } else { range = new import_real_range.RealRange(this.config.lowerInc === import_spec.LOWER_IN, this.config.lower, this.config.upper, this.config.upperInc === import_spec.UPPER_IN); } return this.toRebound(this.config, range); } fromExclusive(value) { this.setLower(value, import_spec.LOWER_EX); return this; } fromInclusive(value) { this.setLower(value, import_spec.LOWER_IN); return this; } fromNegInfinity() { this.setLower(-Infinity, import_spec.LOWER_IN); return this; } fromValue(lower, inclusive) { const lowerInc = inclusive ? import_spec.LOWER_IN : import_spec.LOWER_EX; this.setLower(lower, lowerInc); return this; } intOnly(intOnly) { const int = intOnly ? import_spec.INT_SET : import_spec.REAL_SET; this.setIntReal(int); return this; } integers() { this.setIntReal(import_spec.INT_SET); return this; } reals() { this.setIntReal(import_spec.REAL_SET); return this; } setIntReal(int) { if (int === import_spec.INT_SET && (this.config.lowerInc != null && this.config.lowerInc === import_spec.LOWER_EX || this.config.upperInc != null && this.config.upperInc === import_spec.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 === import_spec.LOWER_EX && this.config.int == import_spec.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 === import_spec.UPPER_EX && this.config.int == import_spec.INT_SET) { throw new Error("Integer bounds must be inclusive"); } this.config.upper = upper; this.config.upperInc = upperInc; } toExclusive(value) { this.setUpper(value, import_spec.UPPER_EX); return this; } toInclusive(value) { this.setUpper(value, import_spec.UPPER_IN); return this; } toPosInfinity() { this.setUpper(Infinity, import_spec.UPPER_IN); return this; } toValue(value, inclusive) { const upperInc = inclusive ? import_spec.UPPER_IN : import_spec.UPPER_EX; this.setUpper(value, upperInc); return this; } }; __name(_ReboundBuilder, "ReboundBuilder"); let ReboundBuilder = _ReboundBuilder; //# sourceMappingURL=rebound-builder.cjs.map