All files / core/src/utilities getNormalizedAspectRatio.ts

55.55% Statements 5/9
50% Branches 1/2
100% Functions 1/1
55.55% Lines 5/9

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 17 18 19 20 21 22 23 24 25      128x             128x 285182x 285182x 285182x                      
import type { Point2 } from '../types';
import isEqual from './isEqual';
 
const EPSILON = 1e10;
 
/**
 * Normalizes a pair of dimensions into a standardized aspect ratio array.
 * @param aspectRatio - An array containing [width, height].
 * @returns A normalized array [w, h] where at least one value is 1.
 */
export const getNormalizedAspectRatio = (aspectRatio: Point2): Point2 => {
  const [width, height] = aspectRatio;
  Eif (isEqual(width, height)) {
    return [1, 1];
  }
 
  const min = Math.min(width, height);
 
  // Normalize by the minimum value and round to handle floating point errors
  const normalizedW = Math.round((width / min) * EPSILON) / EPSILON;
  const normalizedH = Math.round((height / min) * EPSILON) / EPSILON;
 
  return [normalizedW, normalizedH];
};