UNPKG

@rickosborne/rebound

Version:

Rick Osborne's utilities for working with bounded numbers

157 lines (156 loc) 6.48 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_exports = {}; __export(rebound_exports, { Rebound: () => Rebound }); module.exports = __toCommonJS(rebound_exports); var import_foundation = require("@rickosborne/foundation"); var import_guard = require("@rickosborne/guard"); var import_assert_bounded = require("./assert-bounded.cjs"); var import_from_number_bounded = require("./from-number-bounded.cjs"); var import_guard_bounded = require("./guard-bounded.cjs"); var import_integer_from = require("./integer-from.cjs"); var import_integer_generator = require("./integer-generator.cjs"); var import_random_bounded = require("./random-bounded.cjs"); var import_rebound_builder = require("./rebound-builder.cjs"); var import_spec = require("./spec.cjs"); var import_util = require("./util.cjs"); const _Rebound = class _Rebound { static buildType(typeName) { return new import_rebound_builder.ReboundBuilder( {}, (_config, range) => new _Rebound(typeName, range) ); } fnCache = /* @__PURE__ */ new Map(); isInt; isLowerInc; isUpperInc; label; lower; numberType = NaN; outOfBoundsErrorProvider; range; typeName; upper; constructor(typeName, range) { this.range = range; this.typeName = typeName; this.isInt = range.isInt; this.isUpperInc = range.isUpperInc; this.isLowerInc = range.isLowerInc; this.lower = range.lower; this.upper = range.upper; this.label = range.label; this.outOfBoundsErrorProvider = (v, n) => this.outOfRangeError(v, n); } get assert() { return this.assertWith(); } assertWith(options) { const config = this.withOptions(options, "assert"); const guard = (0, import_util.ifIfPresent)(config.ifPresent, this.guardWith({ ifPresent: true }), this.guard); return this.cacheFn(config, () => (0, import_assert_bounded.assertForBounds)(guard, config.errorProvider, config.ifPresent)); } cacheFn(options, compute) { const key = options.defaultName.concat(":", options.fnName, ":", options.errorProvider.name); return (0, import_foundation.computeIfAbsent)(key, this.fnCache, compute); } get ceil() { return this.toIntWith({ strategy: import_integer_from.CEIL }); } get floor() { return this.toIntWith({ strategy: import_integer_from.FLOOR }); } get fromNumber() { return this.fromNumberWith({ ifPresent: false }); } fromNumberWith(options) { const config = this.withOptions(options, "", "FromNumber"); return this.cacheFn(config, () => (0, import_from_number_bounded.fromNumberForBounds)(this.guard, config.errorProvider, config.ifPresent, config.fnName)); } get guard() { return this.guardWith({ ifPresent: false }); } guardWith(options) { const config = this.withOptions(options, "is"); return this.cacheFn( config, () => (0, import_guard_bounded.guardForBounds)(this.range, this.typeName, config.fnName, config.ifPresent) ); } get integers() { return this.integersWith(); } integersWith(options = {}) { return (0, import_integer_generator.integerGenerator)(this.range, options); } outOfRangeError(value, name) { const type = value === null ? "null" : value === void 0 ? "undefined" : typeof value; return (0, import_guard.scrubStackTrace)(new RangeError(`Expected ${name == null ? "" : name.concat(":")} ${this.typeName} ${this.range.toString()}, actual: ${type === "number" ? value : type}`), /at ((?:Rebound[.])?outOfRangeError|buildError)/); } get random() { return this.randomWith(); } randomWith(options = {}) { const defaultName = `random${this.typeName}`; const { fnName = defaultName, rng } = options; return this.cacheFn({ defaultName, errorProvider: this.outOfBoundsErrorProvider, fnName: (options == null ? void 0 : options.fnName) ?? defaultName, ifPresent: false }, () => (0, import_random_bounded.randomBounded)(this.typeName, this.range.label, this.range.isLowerInc, this.range.lower, this.range.isInt, this.range.upper, this.range.isUpperInc, rng, fnName)); } get round() { return this.toIntWith({ strategy: import_integer_from.ROUND }); } toIntWith(options) { const config = this.withIntStrategy(options); const { errorProvider, fnName, ifPresent, strategy } = config; return this.cacheFn(config, () => (0, import_integer_from.integerFrom)(this.typeName, this.range, errorProvider, ifPresent, strategy, fnName)); } get trunc() { return this.toIntWith({ strategy: import_integer_from.TRUNC }); } withIntStrategy(options) { const prefix = typeof (options == null ? void 0 : options.strategy) === "function" ? "toInt" : (options == null ? void 0 : options.strategy) ?? import_integer_from.ROUND; const config = this.withOptions(options, prefix); const strategy = (options == null ? void 0 : options.strategy) ?? import_integer_from.ROUND; return { ...config, strategy }; } withOptions(options, prefix, suffix = "") { const ifPresent = (options == null ? void 0 : options.ifPresent) ?? false; const errorProvider = (options == null ? void 0 : options.errorProvider) ?? this.outOfBoundsErrorProvider; const defaultName = `${prefix}${prefix === "" ? (0, import_foundation.lowerFirst)(this.typeName) : this.typeName}${suffix}${ifPresent ? "IfPresent" : ""}`; const fnName = (options == null ? void 0 : options.fnName) ?? defaultName; return { defaultName, errorProvider, fnName, ifPresent }; } get [import_spec.BOUNDS]() { return this; } }; __name(_Rebound, "Rebound"); let Rebound = _Rebound; //# sourceMappingURL=rebound.cjs.map