Clamp Number
clampNumber
The clampNumber
function restricts a number to fall within a specified range. If the number is lower than the minimum, it returns the minimum; if higher than the maximum, it returns the maximum; otherwise, it returns the original number.
Function Signature
function clampNumber(value: number, min: number, max: number): number;
Parameters
value
: The number to be clamped.min
: The lower bound of the range (inclusive).max
: The upper bound of the range (inclusive).
Return Value
Returns:
min
ifvalue
<min
max
ifvalue
>max
value
if it's betweenmin
andmax
Example Usage
Within Range
import { clampNumber } from 'nhb-toolbox';
console.log(clampNumber(15, 10, 20)); // 15
Below Minimum
console.log(clampNumber(5, 10, 20)); // 10
Above Maximum
console.log(clampNumber(25, 10, 20)); // 20
Edge Cases
console.log(clampNumber(10, 10, 20)); // 10 (equal to min)
console.log(clampNumber(20, 10, 20)); // 20 (equal to max)
Notes
- Order Matters: If
min
>max
, the function will always returnmin
(this is howMath.max(min, Math.min(value, max))
works). - Type Safety: Only accepts
number
type (unlike some functions that acceptNumeric
strings). - Performance: Uses native
Math.max
andMath.min
for optimal performance.
Conclusion
The clampNumber
function is essential for:
- Restricting user input to valid ranges
- Preventing out-of-bounds errors
- Normalizing values in calculations
- Creating responsive design calculations
It's a fundamental utility for any numeric validation or normalization needs.