Clamps integer v between 0 (inclusive) and array length or length (exclusive). Returns value then will always be at least zero, and a valid array index.
v
// Array of length 4const myArray = [`a`, `b`, `c`, `d`];clampIndex(0, myArray); // 0clampIndex(5, 3); // 2 Copy
// Array of length 4const myArray = [`a`, `b`, `c`, `d`];clampIndex(0, myArray); // 0clampIndex(5, 3); // 2
Throws an error if v is not an integer.
For some data it makes sense that data might 'wrap around' if it exceeds the range. For example rotation angle. Consider using wrap for this.
Value to clamp (must be an interger)
Array, or length of bounds (must be an integer)
Clamped value, minimum will be 0, maximum will be one less than length.
length
Clamps integer
v
between 0 (inclusive) and array length or length (exclusive). Returns value then will always be at least zero, and a valid array index.Example: Usage
Throws an error if
v
is not an integer.For some data it makes sense that data might 'wrap around' if it exceeds the range. For example rotation angle. Consider using wrap for this.