Skip to content

Commit

Permalink
Add is.positiveNumber and is.negativeNumber (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-mohc authored Jul 14, 2023
1 parent 3177d11 commit 1284da0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,14 @@ function foo() {
foo();
```

#### .positiveNumber(value)

Check if `value` is a number and is more than 0.

#### .negativeNumber(value)

Check if `value` is a number and is less than 0.

##### .inRange(value, range)

Check if `value` (number) is in the given `range`. The range is an array of two values, lower bound and upper bound, in no specific order.
Expand Down
11 changes: 11 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ is.string = isOfType<string>('string');
const isNumberType = isOfType<number>('number');
is.number = (value: unknown): value is number => isNumberType(value) && !is.nan(value);

is.positiveNumber = (value: unknown): value is number => is.number(value) && value > 0;

is.negativeNumber = (value: unknown): value is number => is.number(value) && value < 0;

is.bigint = isOfType<bigint>('bigint');

// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down Expand Up @@ -498,6 +502,9 @@ export const enum AssertionTypeDescription {
evenInteger = 'even integer',
oddInteger = 'odd integer',

positiveNumber = 'positive number',
negativeNumber = 'negative number',

directInstanceOf = 'T',
inRange = 'in range',

Expand All @@ -511,6 +518,8 @@ type Assert = {
undefined: (value: unknown) => asserts value is undefined;
string: (value: unknown) => asserts value is string;
number: (value: unknown) => asserts value is number;
positiveNumber: (value: unknown) => asserts value is number;
negativeNumber: (value: unknown) => asserts value is number;
bigint: (value: unknown) => asserts value is bigint;
// eslint-disable-next-line @typescript-eslint/ban-types
function_: (value: unknown) => asserts value is Function;
Expand Down Expand Up @@ -609,6 +618,8 @@ export const assert: Assert = {
undefined: (value: unknown): asserts value is undefined => assertType(is.undefined(value), 'undefined', value),
string: (value: unknown): asserts value is string => assertType(is.string(value), 'string', value),
number: (value: unknown): asserts value is number => assertType(is.number(value), 'number', value),
positiveNumber: (value: unknown): asserts value is number => assertType(is.positiveNumber(value), AssertionTypeDescription.positiveNumber, value),
negativeNumber: (value: unknown): asserts value is number => assertType(is.negativeNumber(value), AssertionTypeDescription.negativeNumber, value),
bigint: (value: unknown): asserts value is bigint => assertType(is.bigint(value), 'bigint', value),
// eslint-disable-next-line @typescript-eslint/ban-types
function_: (value: unknown): asserts value is Function => assertType(is.function_(value), 'Function', value),
Expand Down
76 changes: 76 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,82 @@ test('is.number', t => {
testType(t, 'number', ['integer', 'safeInteger', 'infinite']);
});

test('is.positiveNumber', t => {
t.true(is.positiveNumber(6));
t.true(is.positiveNumber(1.4));
t.true(is.positiveNumber(Number.POSITIVE_INFINITY));

t.notThrows(() => {
assert.positiveNumber(6);
});
t.notThrows(() => {
assert.positiveNumber(1.4);
});
t.notThrows(() => {
assert.positiveNumber(Number.POSITIVE_INFINITY);
});

t.false(is.positiveNumber(0));
t.false(is.positiveNumber(-0));
t.false(is.positiveNumber(-6));
t.false(is.positiveNumber(-1.4));
t.false(is.positiveNumber(Number.NEGATIVE_INFINITY));

t.throws(() => {
assert.positiveNumber(0);
});
t.throws(() => {
assert.positiveNumber(-0);
});
t.throws(() => {
assert.positiveNumber(-6);
});
t.throws(() => {
assert.positiveNumber(-1.4);
});
t.throws(() => {
assert.positiveNumber(Number.NEGATIVE_INFINITY);
});
});

test('is.negativeNumber', t => {
t.true(is.negativeNumber(-6));
t.true(is.negativeNumber(-1.4));
t.true(is.negativeNumber(Number.NEGATIVE_INFINITY));

t.notThrows(() => {
assert.negativeNumber(-6);
});
t.notThrows(() => {
assert.negativeNumber(-1.4);
});
t.notThrows(() => {
assert.negativeNumber(Number.NEGATIVE_INFINITY);
});

t.false(is.negativeNumber(0));
t.false(is.negativeNumber(-0));
t.false(is.negativeNumber(6));
t.false(is.negativeNumber(1.4));
t.false(is.negativeNumber(Number.POSITIVE_INFINITY));

t.throws(() => {
assert.negativeNumber(0);
});
t.throws(() => {
assert.negativeNumber(-0);
});
t.throws(() => {
assert.negativeNumber(6);
});
t.throws(() => {
assert.negativeNumber(1.4);
});
t.throws(() => {
assert.negativeNumber(Number.POSITIVE_INFINITY);
});
});

test('is.bigint', t => {
testType(t, 'bigint');
});
Expand Down

0 comments on commit 1284da0

Please sign in to comment.