UNPKG

@handy-common-utils/misc-utils

Version:
90 lines 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rangeIntersection = exports.roundTo = exports.isInRange = exports.clamp = exports.NumberUtils = void 0; class NumberUtils { /** * Constrains a number within specified bounds. * @param num The number to clamp * @param min The minimum value (inclusive) * @param max The maximum value (inclusive) * @returns The clamped value */ static clamp(num, min, max) { return Math.min(Math.max(num, min), max); } /** * Checks if a number is within a specified range (inclusive). * @param num The number to check * @param min The minimum value * @param max The maximum value * @returns True if the number is within range */ static isInRange(num, min, max) { return num >= min && num <= max; } /** * Rounds a number to a specified number of decimal places. * @param num The number to round * @param precision The number of decimal places (default: 0) * @returns Rounded number */ static roundTo(num, precision = 0) { const factor = Math.pow(10, precision); return Math.round(num * factor) / factor; } /** * Calculates the intersection between two ranges. * @param min1 Minimum value of the first range * @param max1 Maximum value of the first range * @param min2 Minimum value of the second range * @param max2 Maximum value of the second range * @returns The intersection as a tuple [min, max], or undefined if there is no intersection. */ static rangeIntersection(min1, max1, min2, max2) { const r1Min = Math.min(min1, max1); const r1Max = Math.max(min1, max1); const r2Min = Math.min(min2, max2); const r2Max = Math.max(min2, max2); const intersectionMin = Math.max(r1Min, r2Min); const intersectionMax = Math.min(r1Max, r2Max); if (intersectionMin <= intersectionMax) { return [intersectionMin, intersectionMax]; } return undefined; } } exports.NumberUtils = NumberUtils; // Export functions as constants for convenience /** * Constrains a number within specified bounds. * @param num The number to clamp * @param min The minimum value * @param max The maximum value * @returns The clamped value */ exports.clamp = NumberUtils.clamp; /** * Checks if a number is within a specified range (inclusive). * @param num The number to check * @param min The minimum value * @param max The maximum value * @returns True if the number is within range */ exports.isInRange = NumberUtils.isInRange; /** * Rounds a number to a specified number of decimal places. * @param num The number to round * @param precision The number of decimal places (default: 0) * @returns Rounded number */ exports.roundTo = NumberUtils.roundTo; /** * Calculates the intersection between two ranges. * @param min1 Minimum value of the first range * @param max1 Maximum value of the first range * @param min2 Minimum value of the second range * @param max2 Maximum value of the second range * @returns The intersection as a tuple [min, max], or undefined if there is no intersection. */ exports.rangeIntersection = NumberUtils.rangeIntersection; //# sourceMappingURL=number.js.map