Skip to content

Commit

Permalink
Make is.number(NaN) return false (#90)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
kainiedziela and sindresorhus committed Jun 30, 2019
1 parent ffc6ce4 commit f04dffa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`.

Expand Down
5 changes: 4 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f04dffa

Please sign in to comment.