All files / math clamp.ts

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

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                        16x    
/**
 * Restricts a number to an interval.
 *
 * @param value - The number to restrict.
 * @param min - The lower bound. Defaults to `0`.
 * @param max - The upper bound. Defaults to `1`.
 * @returns `value` within the bounds, or the nearest bound.
 * @example
 * clamp(15, 0, 10); // 10
 * clamp(1.5); // 1
 */
export function clamp(value: number, min = 0, max = 1): number {
  return Math.min(Math.max(value, min), max);
}