Version: v0.7.1 - Beta.  We welcome contributors & feedback.

Number.clamp

clamp($min, $max) -> number

Description

Ensure that the number is between $min and $max (inclusive).

If outside the range, it returns the nearest boundary ($min or $max).

Otherwise, it returns the number itself.

$num = 5
$num.clamp(1, 10)  //= 5  (within boundary of 1-10)

$num = 20
$num.clamp(1, 10)  //= 10 (max of 10)

$num = -20
$num.clamp(1, 10)  //= 1  (min of 1)

See Also