Function wrap

  • Wrap a value around a specified range. When the value exceeds the max, it wraps back to min, and when it's below min, it wraps around to max.

    Parameters

    • min: number

      The minimum value of the range.

    • max: number

      The maximum value of the range.

    • value: number

      The value to wrap around the range.

    Returns number

    Example

    wrap(0, 3, 0); // => 0
    wrap(0, 3, 1); // => 1
    wrap(0, 3, 2); // => 2
    wrap(0, 3, 3); // => 0 (wraps back to the start)
    wrap(0, 3, -1); // => 2 (wraps from below to the end)
    wrap(0, 3, -2); // => 1
    wrap(0, 3, -3); // => 0