Skip to content

Commit 1852e6b

Browse files
authored
feat(utils): Add isNaN function (#4759)
This adds a function, `isNaN`, to our `is` module. While it's true that there is a built-in function of the same name, it assumes it's being passed a number (which messes up types), whereas the function introduced here makes no such assumptions.
1 parent 20b3e38 commit 1852e6b

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/utils/src/is.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ export function isThenable(wat: any): wat is PromiseLike<any> {
146146
export function isSyntheticEvent(wat: unknown): boolean {
147147
return isPlainObject(wat) && 'nativeEvent' in wat && 'preventDefault' in wat && 'stopPropagation' in wat;
148148
}
149+
150+
/**
151+
* Checks whether given value is NaN
152+
* {@link isNaN}.
153+
*
154+
* @param wat A value to be checked.
155+
* @returns A boolean representing the result.
156+
*/
157+
export function isNaN(wat: unknown): boolean {
158+
return typeof wat === 'number' && wat !== wat;
159+
}
160+
149161
/**
150162
* Checks whether given value's type is an instance of provided constructor.
151163
* {@link isInstanceOf}.

packages/utils/test/is.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { isDOMError, isDOMException, isError, isErrorEvent, isInstanceOf, isPrimitive, isThenable } from '../src/is';
1+
import {
2+
isDOMError,
3+
isDOMException,
4+
isError,
5+
isErrorEvent,
6+
isInstanceOf,
7+
isNaN,
8+
isPrimitive,
9+
isThenable,
10+
} from '../src/is';
211
import { supportsDOMError, supportsDOMException, supportsErrorEvent } from '../src/supports';
312
import { resolvedSyncPromise } from '../src/syncpromise';
413

@@ -110,3 +119,18 @@ describe('isInstanceOf()', () => {
110119
expect(isInstanceOf(new Error('wat'), undefined)).toEqual(false);
111120
});
112121
});
122+
123+
describe('isNaN()', () => {
124+
test('should work as advertised', () => {
125+
expect(isNaN(NaN)).toEqual(true);
126+
127+
expect(isNaN(null)).toEqual(false);
128+
expect(isNaN(true)).toEqual(false);
129+
expect(isNaN('foo')).toEqual(false);
130+
expect(isNaN(42)).toEqual(false);
131+
expect(isNaN({})).toEqual(false);
132+
expect(isNaN([])).toEqual(false);
133+
expect(isNaN(new Error('foo'))).toEqual(false);
134+
expect(isNaN(new Date())).toEqual(false);
135+
});
136+
});

0 commit comments

Comments
 (0)