forked from orbit-oss/is
Fix isInRange silently returning false when range contains NaN
This commit is contained in:
parent
47415dc46a
commit
ac46b5400d
2 changed files with 12 additions and 0 deletions
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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]);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue