The clamp
function from Lodash is used to clamp a given number within the inclusive lower
and upper
bounds. If the given number
is smaller than lower
, it returns lower
. If the given number
is greater than upper
, it returns upper
. Otherwise, it returns the given number
.
Here's an example usage of the clamp
function in Javascript:
index.tsx179 chars10 lines
In this example, the clamp
function is imported from Lodash using the require statement, and it is called with number
, lower
, and upper
as arguments. The result is stored in the clampedNumber
variable, which is printed to the console.
If we were to call clamp
with a number
that is outside of the lower
and upper
bounds:
index.tsx180 chars10 lines
The clamp
function will return lower
because number
is below the lower
bound.
If we were to call clamp
with a number
that is greater than upper
:
index.tsx181 chars10 lines
The clamp
function will return upper
because number
is above the upper
bound.
gistlibby LogSnag