Make is.number(NaN) return false (#90)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
This commit is contained in:
Kai Niedziela 2019-06-30 10:09:07 +02:00 committed by Sindre Sorhus
parent ffc6ce4586
commit f04dffa575
3 changed files with 9 additions and 3 deletions

View file

@ -68,6 +68,9 @@ All the below methods accept a value and returns a boolean for whether the value
##### .null(value)
##### .string(value)
##### .number(value)
Note: `is.number(NaN)` returns `false`. This intentionally deviates from `typeof` behavior to increase user-friendliness of `is` type checks.
##### .boolean(value)
##### .symbol(value)
##### .bigint(value)
@ -83,7 +86,7 @@ Keep in mind that [functions are objects too](https://developer.mozilla.org/en-U
##### .numericString(value)
Returns `true` for a string that represents a number. For example, `'42'` and `'-8'`.
Returns `true` for a string that represents a number satisfying `is.number`, for example, `'42'` and `'-8.3'`.
Note: `'NaN'` returns `false`, but `'Infinity'` and `'-Infinity'` return `true`.

View file

@ -117,7 +117,10 @@ const isObject = (value: unknown): value is object => typeof value === 'object';
is.undefined = isOfType<undefined>('undefined');
is.string = isOfType<string>('string');
is.number = isOfType<number>('number');
const isNumberType = isOfType<number>('number');
is.number = (value: unknown): value is number => isNumberType(value) && !is.nan(value);
is.bigint = isOfType<bigint>('bigint');
// eslint-disable-next-line @typescript-eslint/ban-types

View file

@ -493,7 +493,7 @@ test('is.string', t => {
});
test('is.number', t => {
testType(t, 'number', ['nan', 'integer', 'safeInteger', 'infinite']);
testType(t, 'number', ['integer', 'safeInteger', 'infinite']);
});
// TODO: Nodejs 10 only