clamp (for BigInts) es-toolkit
Restricts a BigInt to a given range.
This function is available exclusively from es-toolkit/bigint to avoid potential conflicts with similar functions for other numeric types.
Usage
clamp(value, maximum)
Use clamp with two arguments when you only want an upper limit. Anything above the maximum comes back as the maximum, and anything else is returned unchanged.
Parameters
The BigInt to clamp.
The upper bound, inclusive.
Returns
Returns the BigInt, capped at the maximum.
clamp(value, minimum, maximum)
Use clamp with three arguments when you want both a lower and an upper limit. Math.min and Math.max cannot accept BigInts, so this is the way to do it.
Because BigInts are compared exactly, bounds far past Number.MAX_SAFE_INTEGER still behave the way you would expect.
Parameters
The BigInt to clamp.
The lower bound, inclusive.
The upper bound, inclusive.
Returns
Returns the BigInt, constrained to the range.
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.