ixfx
    Preparing search index...

    Function clampIndex

    • 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.

      Parameters

      • v: number

        Value to clamp (must be an interger)

      • arrayOrLength: number | readonly any[]

        Array, or length of bounds (must be an integer)

      Returns number

      Clamped value, minimum will be 0, maximum will be one less than length.

      // Array of length 4
      const myArray = [`a`, `b`, `c`, `d`];
      clampIndex(0, myArray); // 0
      clampIndex(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.