From 9412509c42e1059255f261a27bfa64b7d228ae96 Mon Sep 17 00:00:00 2001 From: Pedr Date: Sat, 27 Jul 2024 16:10:39 +0100 Subject: [PATCH] fix: Round point coordinates before validating proximity --- src/utils/math.js | 1 + src/utils/validation.js | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils/math.js b/src/utils/math.js index 6d70292..308144e 100644 --- a/src/utils/math.js +++ b/src/utils/math.js @@ -11,6 +11,7 @@ const roundToN = (n, value) => Number(value.toFixed(n)) // ----------------------------------------------------------------------------- export const roundTo10 = (value) => roundToN(10, value) +export const roundTo5 = (value) => roundToN(5, value) export const binomial = (n, k) => { if (n === 0) { diff --git a/src/utils/validation.js b/src/utils/validation.js index 0c7eb07..a6a52b0 100644 --- a/src/utils/validation.js +++ b/src/utils/validation.js @@ -1,3 +1,5 @@ +import { mapObj } from './functional' +import { roundTo5 } from './math' import { isArray, isInt, isNil, isPlainObj } from './types' // ----------------------------------------------------------------------------- @@ -5,7 +7,14 @@ import { isArray, isInt, isNil, isPlainObj } from './types' // ----------------------------------------------------------------------------- const getPointsAreSame = (point1, point2) => { - return point1.x === point2.x && point2.y === point2.y + // Round the points to 5 decimal places to avoid rounding issues where the + // values are fractionally different + const roundedPoint1 = mapObj(roundTo5, point1) + const roundedPoint2 = mapObj(roundTo5, point2) + + return ( + roundedPoint1.x === roundedPoint2.x && roundedPoint1.y === roundedPoint2.y + ) } // -----------------------------------------------------------------------------