From 480a2bfbd69197561c20263c0f6ea3175a0a19a7 Mon Sep 17 00:00:00 2001 From: Pascal Syma Date: Fri, 13 Dec 2024 14:36:41 +0100 Subject: [PATCH] Add isInteger coordinate utils --- utils/coordinates.test.ts | 13 ++++++++++++- utils/coordinates.ts | 8 ++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/utils/coordinates.test.ts b/utils/coordinates.test.ts index 7ac9892..da0cd50 100644 --- a/utils/coordinates.test.ts +++ b/utils/coordinates.test.ts @@ -4,7 +4,8 @@ import { findCoordsOfDigit, getAdjacent, inBounds, - isCoordinateEqual + isCoordinateEqual, + isInteger } from './coordinates.ts'; describe('coordinates utils', () => { @@ -101,4 +102,14 @@ describe('coordinates utils', () => { expect(actualSevenRegex[0]).toEqual([1, 2]); expect(actualSevenRegex[1]).toEqual([2, 3]); }); + + it('isInteger', () => { + expect(isInteger([1, 2])).toBeTrue(); + expect(isInteger([1, 2, 3])).toBeTrue(); + expect(isInteger([0, 0])).toBeTrue(); + expect(isInteger([0, 10.001])).toBeFalse(); + expect(isInteger([472.2948292, 10.001])).toBeFalse(); + expect(isInteger([-472.2948292, 10.001])).toBeFalse(); + expect(isInteger([-472.2948292, -10.001])).toBeFalse(); + }); }); diff --git a/utils/coordinates.ts b/utils/coordinates.ts index 5c79c57..2591eac 100644 --- a/utils/coordinates.ts +++ b/utils/coordinates.ts @@ -36,6 +36,14 @@ export function isCoordinateEqual( return b[0] === a[0] && b[1] === a[1]; } +/** + * Check if a xy-coordinate has integer coefficients. + * @param coordinate Coordinate + */ +export function isInteger(coordinate: Readonly) { + return coordinate[0] % 1 === 0 && coordinate[1] % 1 === 0; +} + export type DirectNeighbours = 'top' | 'left' | 'right' | 'down'; export type DiagonalNeighbours =