UNPKG

super-utils-plus

Version:

A superior alternative to Lodash with improved performance, TypeScript support, and developer experience

32 lines (31 loc) 678 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.clamp = clamp; /** * Clamps a number within the inclusive lower and upper bounds. * * @param value - The number to clamp * @param lower - The lower bound * @param upper - The upper bound * @returns The clamped number * * @example * ```ts * clamp(-10, -5, 5); * // => -5 * * clamp(10, -5, 5); * // => 5 * * clamp(3, -5, 5); * // => 3 * ``` */ function clamp(value, lower, upper) { const min = Math.min(lower, upper); const max = Math.max(lower, upper); if (Number.isNaN(value)) { return Number.NaN; } return Math.min(Math.max(value, min), max); }