From ac46b5400d7ae00fcc001467ddfda5f9624c4ce8 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 8 Apr 2026 14:54:37 +0700 Subject: [PATCH] Fix `isInRange` silently returning false when range contains `NaN` --- source/index.ts | 4 ++++ test/test.ts | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/source/index.ts b/source/index.ts index 3131d02..9bfe2e6 100644 --- a/source/index.ts +++ b/source/index.ts @@ -599,6 +599,10 @@ export function isInRange(value: number, range: number | [number, number]): valu } if (isArray(range) && range.length === 2) { + if (Number.isNaN(range[0]) || Number.isNaN(range[1])) { + throw new TypeError(`Invalid range: ${JSON.stringify(range)}`); + } + return value >= Math.min(...range) && value <= Math.max(...range); } diff --git a/test/test.ts b/test/test.ts index 31e661b..750858e 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1274,6 +1274,14 @@ test('is.inRange', () => { is.inRange(0, [1, 2, 3]); }); + assert.throws(() => { + is.inRange(5, [NaN, 10]); + }, TypeError); + + assert.throws(() => { + is.inRange(5, [0, NaN]); + }, TypeError); + assert.doesNotThrow(() => { isAssert.inRange(x, [0, 5]); });