Skip to content

feat(utils): Add isNaN function #4759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/utils/src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ export function isThenable(wat: any): wat is PromiseLike<any> {
export function isSyntheticEvent(wat: unknown): boolean {
return isPlainObject(wat) && 'nativeEvent' in wat && 'preventDefault' in wat && 'stopPropagation' in wat;
}

/**
* Checks whether given value is NaN
* {@link isNaN}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isNaN(wat: unknown): boolean {
return typeof wat === 'number' && wat !== wat;
}

/**
* Checks whether given value's type is an instance of provided constructor.
* {@link isInstanceOf}.
Expand Down
26 changes: 25 additions & 1 deletion packages/utils/test/is.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { isDOMError, isDOMException, isError, isErrorEvent, isInstanceOf, isPrimitive, isThenable } from '../src/is';
import {
isDOMError,
isDOMException,
isError,
isErrorEvent,
isInstanceOf,
isNaN,
isPrimitive,
isThenable,
} from '../src/is';
import { supportsDOMError, supportsDOMException, supportsErrorEvent } from '../src/supports';
import { resolvedSyncPromise } from '../src/syncpromise';

Expand Down Expand Up @@ -110,3 +119,18 @@ describe('isInstanceOf()', () => {
expect(isInstanceOf(new Error('wat'), undefined)).toEqual(false);
});
});

describe('isNaN()', () => {
test('should work as advertised', () => {
expect(isNaN(NaN)).toEqual(true);

expect(isNaN(null)).toEqual(false);
expect(isNaN(true)).toEqual(false);
expect(isNaN('foo')).toEqual(false);
expect(isNaN(42)).toEqual(false);
expect(isNaN({})).toEqual(false);
expect(isNaN([])).toEqual(false);
expect(isNaN(new Error('foo'))).toEqual(false);
expect(isNaN(new Date())).toEqual(false);
});
});