Fix isInRange silently returning false when range contains NaN

This commit is contained in:
Sindre Sorhus 2026-04-08 14:54:37 +07:00
parent 47415dc46a
commit ac46b5400d
2 changed files with 12 additions and 0 deletions

View file

@ -599,6 +599,10 @@ export function isInRange(value: number, range: number | [number, number]): valu
} }
if (isArray(range) && range.length === 2) { 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); return value >= Math.min(...range) && value <= Math.max(...range);
} }

View file

@ -1274,6 +1274,14 @@ test('is.inRange', () => {
is.inRange(0, [1, 2, 3]); is.inRange(0, [1, 2, 3]);
}); });
assert.throws(() => {
is.inRange(5, [NaN, 10]);
}, TypeError);
assert.throws(() => {
is.inRange(5, [0, NaN]);
}, TypeError);
assert.doesNotThrow(() => { assert.doesNotThrow(() => {
isAssert.inRange(x, [0, 5]); isAssert.inRange(x, [0, 5]);
}); });