All files / math ratio.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                      5x    
/**
 * Divides a value by a total, without `NaN` or `Infinity`. Not clamped.
 *
 * @param value - The part.
 * @param total - The whole.
 * @returns `value / total`; `0` when `total` is `0`.
 * @example
 * ratio(5, 10); // 0.5
 * ratio(5, 0); // 0
 */
export function ratio(value: number, total: number): number {
  return total === 0 ? 0 : value / total;
}