All files is-closed.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 1/1
100% Lines 8/8

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                        1x 58x 47x 47x 47x 47x 11x 11x  
import { type Polygon } from './@types/geometry.ts';
 
/**
 * Determines whether a given polygon is closed.
 *
 * A polygon is considered closed if it has more than one point and
 * its first and last points have identical `x` and `y` coordinates.
 * @param polygon - An array of points representing the polygon, where each point has `x` and `y` properties.
 * @returns `true` if the polygon is closed; otherwise, `false`.
 * @group Geometry
 * @category Polygon
 */
export function isClosed(polygon: Polygon): boolean {
  if (polygon.length > 1) {
    const first = polygon.at(0)!;
    const last = polygon.at(-1)!;
    return first.x === last.x && first.y === last.y;
  }
  return true;
}