diff --git a/readme.md b/readme.md index 5bfdd72..c3c5adb 100644 --- a/readme.md +++ b/readme.md @@ -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`. diff --git a/source/index.ts b/source/index.ts index e8c226e..33b7896 100644 --- a/source/index.ts +++ b/source/index.ts @@ -117,7 +117,10 @@ const isObject = (value: unknown): value is object => typeof value === 'object'; is.undefined = isOfType('undefined'); is.string = isOfType('string'); -is.number = isOfType('number'); + +const isNumberType = isOfType('number'); +is.number = (value: unknown): value is number => isNumberType(value) && !is.nan(value); + is.bigint = isOfType('bigint'); // eslint-disable-next-line @typescript-eslint/ban-types diff --git a/test/test.ts b/test/test.ts index cde7374..748ce7a 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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