-
-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
IsInteger
and IsFloat
, fix Integer
and Float
handing with…
… edge case (#857) Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
c1d2e0a
commit f5b09de
Showing
8 changed files
with
178 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type {Zero} from './numeric'; | ||
|
||
/** | ||
Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`. | ||
It returns `false` for `Infinity`. | ||
Use-case: | ||
- If you want to make a conditional branch based on the result of whether a number is a float or not. | ||
@example | ||
``` | ||
type Float = IsFloat<1.5>; | ||
//=> true | ||
type IntegerWithDecimal = IsInteger<1.0>; | ||
//=> false | ||
type NegativeFloat = IsInteger<-1.5>; | ||
//=> true | ||
type Infinity_ = IsInteger<Infinity>; | ||
//=> false | ||
``` | ||
*/ | ||
export type IsFloat<T> = | ||
T extends number | ||
? `${T}` extends `${infer _Sign extends '' | '-'}${number}.${infer Decimal extends number}` | ||
? Decimal extends Zero | ||
? false | ||
: true | ||
: false | ||
: false; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type {Not} from './internal'; | ||
import type {IsFloat} from './is-float'; | ||
import type {PositiveInfinity, NegativeInfinity} from './numeric'; | ||
|
||
/** | ||
Returns a boolean for whether the given number is a integer, like `-5`, `1.0` or `100`. | ||
Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/IsInteger) but for types. | ||
Use-case: | ||
- If you want to make a conditional branch based on the result of whether a number is a intrger or not. | ||
@example | ||
``` | ||
type Integer = IsInteger<1>; | ||
//=> true | ||
type IntegerWithDecimal = IsInteger<1.0>; | ||
//=> true | ||
type NegativeInteger = IsInteger<-1>; | ||
//=> true | ||
type Float = IsInteger<1.5>; | ||
//=> false | ||
// Supports non-decimal numbers | ||
type OctalInteger: IsInteger<0o10>; | ||
//=> true | ||
type BinaryInteger: IsInteger<0b10>; | ||
//=> true | ||
type HexadecimalInteger: IsInteger<0x10>; | ||
//=> true | ||
``` | ||
*/ | ||
export type IsInteger<T> = | ||
T extends bigint | ||
? true | ||
: T extends number | ||
? number extends T | ||
? false | ||
: T extends PositiveInfinity | NegativeInfinity | ||
? false | ||
: Not<IsFloat<T>> | ||
: false; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {expectType} from 'tsd'; | ||
import type {IsFloat, PositiveInfinity} from '../index'; | ||
|
||
expectType<false>({} as IsFloat<0>); | ||
expectType<false>({} as IsFloat<1>); | ||
expectType<false>({} as IsFloat<1.0>); // eslint-disable-line unicorn/no-zero-fractions | ||
expectType<true>({} as IsFloat<1.5>); | ||
expectType<false>({} as IsFloat<-1>); | ||
expectType<false>({} as IsFloat<number>); | ||
expectType<false>({} as IsFloat<0o10>); | ||
expectType<false>({} as IsFloat<1n>); | ||
expectType<false>({} as IsFloat<0n>); | ||
expectType<false>({} as IsFloat<0b10>); | ||
expectType<false>({} as IsFloat<0x10>); | ||
expectType<false>({} as IsFloat<1e+100>); | ||
expectType<false>({} as IsFloat<PositiveInfinity>); | ||
expectType<false>({} as IsFloat<typeof Number.POSITIVE_INFINITY>); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {expectType} from 'tsd'; | ||
import type {IsInteger, PositiveInfinity} from '../index'; | ||
|
||
expectType<true>({} as IsInteger<0>); | ||
expectType<true>({} as IsInteger<1>); | ||
expectType<true>({} as IsInteger<1.0>); // eslint-disable-line unicorn/no-zero-fractions | ||
expectType<false>({} as IsInteger<1.5>); | ||
expectType<true>({} as IsInteger<-1>); | ||
expectType<false>({} as IsInteger<number>); | ||
expectType<true>({} as IsInteger<0o10>); | ||
expectType<true>({} as IsInteger<1n>); | ||
expectType<true>({} as IsInteger<0n>); | ||
expectType<true>({} as IsInteger<0b10>); | ||
expectType<true>({} as IsInteger<0x10>); | ||
expectType<true>({} as IsInteger<1e+100>); | ||
expectType<false>({} as IsInteger<PositiveInfinity>); | ||
expectType<false>({} as IsInteger<typeof Number.POSITIVE_INFINITY>); |
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