Last active
November 27, 2022 23:48
-
-
Save yano3nora/8a6bbda1cc70e7aaddd3b31a9cce8c31 to your computer and use it in GitHub Desktop.
[js: is number?] #js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect, test } from '@jest/globals' | |
import { isNumber } from 'libs/utils/is-number' | |
test('string literal be fail', () => { | |
expect(isNumber('1')).toBe(false) | |
}) | |
test('number literal be true', () => { | |
expect(isNumber(1)).toBe(true) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* isNumber - 数値かどうかを検査する | |
* | |
* ## true | |
* isNumber(0) | |
* isNumber(1) | |
* isNumber(-1) | |
* isNumber(1.5) | |
* isNumber(-1.5) | |
* | |
* ## false | |
* isNumber(null) | |
* isNumber(undefined) | |
* isNumber(NaN) | |
* isNumber('1') | |
* isNumber(true) | |
* isNumber(false) | |
* isNumber(new Date()) | |
* isNumber(Infinity) | |
* isNumber(-Infinity) | |
* isNumber([]) | |
* isNumber({}) | |
* isNumber(Symbol('foo')) | |
* | |
* ### BigInt 未対応 | |
* isNumber(2n ** 53n) // false | |
*/ | |
export const isNumber = (value: any) => { | |
return ((typeof value === 'number') && (isFinite(value))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment