UNPKG

@rickosborne/rebound

Version:

Rick Osborne's utilities for working with bounded numbers

60 lines (59 loc) 2.8 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 scale_bounded_exports = {}; __export(scale_bounded_exports, { scaleBounded: () => scaleBounded }); module.exports = __toCommonJS(scale_bounded_exports); var import_foundation = require("@rickosborne/foundation"); var import_effective_range = require("./effective-range.cjs"); var import_typed_function = require("./typed-function.cjs"); const scaleBounded = /* @__PURE__ */ __name((inputBounds, outputBounds, ifPresent, fnName = `${(0, import_foundation.lowerFirst)(outputBounds.typeName)}From${inputBounds.typeName}`) => { const inputRange = (0, import_effective_range.effectiveRange)(inputBounds); if (inputRange === 0) { throw new RangeError("Input range must be > 0"); } const outputRange = (0, import_effective_range.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) { import_foundation.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 (0, import_typed_function.addTypedProperties)(fn, outputBounds, outputBounds.typeName, fnName); }, "scaleBounded"); //# sourceMappingURL=scale-bounded.cjs.map