All files / math wrap.ts

100% Statements 2/2
100% Branches 0/0
100% Functions 1/1
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16                        8x 8x    
/**
 * Wraps a number into [min, max[ with an always-positive modulo, like a heading wrapping at 360°.
 *
 * @param value - The number to wrap.
 * @param min - The included bound.
 * @param max - The excluded bound.
 * @returns The equivalent value in [min, max[.
 * @example
 * wrap(370, 0, 360); // 10
 * wrap(-10, 0, 360); // 350
 */
export function wrap(value: number, min: number, max: number): number {
  const span = max - min;
  return ((((value - min) % span) + span) % span) + min;
}