UNPKG

@rickosborne/rebound

Version:

Rick Osborne's utilities for working with bounded numbers

41 lines (40 loc) 1.78 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { con, lowerFirst } from "@rickosborne/foundation"; import { effectiveRange } from "./effective-range.mjs"; import { addTypedProperties } from "./typed-function.mjs"; const scaleBounded = /* @__PURE__ */ __name((inputBounds, outputBounds, ifPresent, fnName = `${lowerFirst(outputBounds.typeName)}From${inputBounds.typeName}`) => { const inputRange = effectiveRange(inputBounds); if (inputRange === 0) { throw new RangeError("Input range must be > 0"); } const outputRange = effectiveRange(outputBounds); const inputShift = inputBounds.lower + (inputBounds.isLowerInc ? 0 : inputBounds.isInt ? 1 : Number.MIN_VALUE); const outputShift = outputBounds.lower + (outputBounds.isLowerInc ? 0 : outputBounds.isInt ? 1 : Number.MIN_VALUE); if (outputRange === 0) { con.warn("Effective output range is 0. All outputs will be the same."); if (ifPresent) { return ((value) => value == null ? void 0 : outputShift); } else { return (() => outputShift); } } const factor = outputRange / inputRange; let convert; if (outputBounds.isInt) { convert = /* @__PURE__ */ __name((input) => Math.trunc((input - inputShift) * factor + outputShift), "convert"); } else { convert = /* @__PURE__ */ __name((input) => (input - inputShift) * factor + outputShift, "convert"); } let fn; if (ifPresent) { fn = /* @__PURE__ */ __name(((input) => input == null ? void 0 : convert(input)), "fn"); } else { fn = convert; } return addTypedProperties(fn, outputBounds, outputBounds.typeName, fnName); }, "scaleBounded"); export { scaleBounded }; //# sourceMappingURL=scale-bounded.mjs.map