All files / utils NumberUtil.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 2/2
100% Lines 7/7

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  2x       2x 426x 426x 426x 426x 426x      
/** @internal */
export namespace NumberUtil {
  /** @arg min An integer < max
      @arg max An integer > min
      @return A value wrapped to the domain [min, max). */
  export function wrap(val: number, min: number, max: number): number {
    const range = max - min // range ∈ [0, +∞).
    const x = (val - min) % range // Subtract min and wrap to x ∈ (-range, range).
    const y = x + range // Translate to y ∈ (0, 2 * range).
    const z = y % range // Wrap to z ∈ [0, range).
    return z + min // Add min to return ∈ [min, max).
  }
}