From b9dcc690961f34b22517d367ad9a7c68babfe43a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= Date: Wed, 19 Nov 2025 21:03:03 +0100 Subject: [PATCH] Add a 'numberPrecision' option If comparing numbers, rounding errors may be allowed up to a specified precision. Usage: deepEqualCheck([ 1, 2 ], [ 1, 2.000000001 ], { numberPrecision: 0.000001 }) -> true --- src/index.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/index.ts b/src/index.ts index 1f2cbd7..d927707 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,10 @@ function deepEqualCore( return options.nanEqual; } + if (typeof a === 'number' && typeof b === 'number' && options.numberPrecision && Math.abs(a - b) < options.numberPrecision) { + return true; + } + // Fast path: if both are primitives (not objects/functions), return false if ( (a === null || typeof a !== 'object' && typeof a !== 'function') && @@ -303,6 +307,7 @@ export function deepEqualCheck( checkPrototypes: options.checkPrototypes ?? false, strictZero: options.strictZero ?? false, maxDepth: options.maxDepth ?? 1000, + numberPrecision: options.numberPrecision ?? false, }; return deepEqualCore(a, b, opts, new WeakMap(), 0);