@rickosborne/rebound
Version:
Rick Osborne's utilities for working with bounded numbers
36 lines (35 loc) • 1.56 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { lowerFirst } from "@rickosborne/foundation";
import { scrubStackTrace } from "@rickosborne/guard";
import { addTypedProperties } from "./typed-function.mjs";
const ROUND = "round";
const CEIL = "ceil";
const TRUNC = "trunc";
const FLOOR = "floor";
function boundedIntFromNumber(isLowerInc, lower, upper, isUpperInc, ifPresent, errorProvider, strategy, value, name) {
if (value != null && (isLowerInc ? value >= lower : value > lower) && (isUpperInc ? value <= upper : value < upper)) {
return strategy(value);
}
if (value == null && ifPresent) {
return void 0;
}
const error = errorProvider(value, name);
throw scrubStackTrace(error, "boundedIntFromNumber");
}
__name(boundedIntFromNumber, "boundedIntFromNumber");
function integerFrom(typeName, bounds, errorProvider, ifPresent, strategy = ROUND, fnName = `${lowerFirst(typeName)}From${typeof strategy === "function" ? strategy.name : strategy}${ifPresent ? "IfPresent" : ""}`) {
const toInt = typeof strategy === "function" ? strategy : Math[strategy];
const boundedIntFrom = boundedIntFromNumber.bind(void 0, bounds.isLowerInc, bounds.lower, bounds.upper, bounds.isUpperInc, ifPresent, errorProvider, toInt);
return addTypedProperties(boundedIntFrom, bounds, typeName, fnName);
}
__name(integerFrom, "integerFrom");
export {
CEIL,
FLOOR,
ROUND,
TRUNC,
boundedIntFromNumber,
integerFrom
};
//# sourceMappingURL=integer-from.mjs.map