Skip to content

Commit

Permalink
Add isInfinite() (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
younho9 authored Jan 3, 2022
1 parent 9b2d00e commit 076d9ff
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {isDefined} from 'ts-extras';
- [`isDefined`](source/is-defined.ts) - Check whether a value is defined (not `null` or `undefined`).
- [`isEmpty`](source/is-empty.ts) - Check whether an array is empty.
- [`assertError`](source/assert-error.ts) - Assert that the given value is an `Error`.
- [`isInfinite`](source/is-infinite.ts) - Check whether a value is infinite.

**Improved builtin**

Expand Down
1 change: 1 addition & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {assertError} from './assert-error.js';
export {isDefined} from './is-defined.js';
export {isEmpty} from './is-empty.js';
export {isFinite} from './is-finite.js';
export {isInfinite} from './is-infinite.js';
export {isInteger} from './is-integer.js';
export {isSafeInteger} from './is-safe-integer.js';
export {objectEntries} from './object-entries.js';
Expand Down
10 changes: 10 additions & 0 deletions source/is-infinite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {NegativeInfinity, PositiveInfinity} from 'type-fest';

/**
Check whether a value is infinite.
@category Type guard
*/
export function isInfinite(value: unknown): value is NegativeInfinity | PositiveInfinity {
return !Number.isNaN(value) && !Number.isFinite(value);
}
18 changes: 18 additions & 0 deletions test/is-infinite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import test from 'ava';
import {expectTypeOf} from 'expect-type';
import {NegativeInfinity, PositiveInfinity} from 'type-fest';
import {isInfinite} from '../source/index.js';

test('isInfinite()', t => {
t.false(isInfinite(123));
t.true(isInfinite(Number.POSITIVE_INFINITY));
t.true(isInfinite(Number.NEGATIVE_INFINITY));

const number_ = 123 as number;

if (isInfinite(number_)) {
expectTypeOf<PositiveInfinity | NegativeInfinity>(number_);
} else {
expectTypeOf<number>(number_);
}
});

0 comments on commit 076d9ff

Please sign in to comment.