1.0.0 • Published 1 year ago

@miyauci/assertion v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

assertion

deno land GitHub release (latest by date) codecov GitHub

test NPM

Assertion collection for JavaScript data.

Module structure and capability

Module can be divided into two categories.

Top-type module

Top-type module can accept any JavaScript data. In other words, it accepts the unknown type.

The module directly under namespace is it.

Sub-type module

Sub-type modules are modules that perform type-dependent operations. It can use type-specific methods and compare values.

For example, the module under number is a sub-type module that takes a number type as an argument.

Common

All function signatures have an asserts specifier.

All modules throw an error if the assert fails. And the errors are fully customizable.

Error message

No opinion on error message. Nothing by default.

You can give message.

import { assertNumber } from "https://deno.land/x/assertion@$VERSION/assert_number.ts";

declare const input: unknown;

assertNumber(input, `must be number`);

Error constructor

No opinion on error constructor.

By default, use Error constructor.

To throw RangeError instead, do the following.

import { assertNonNegativeInteger } from "https://deno.land/x/assertion@$VERSION/number/assert_non_negative_integer.ts";

declare const input: number;

assertNonNegativeInteger(input, `must be non-negative integer`, RangeError);

assertArray

badge

Assert the input is array.

import { assertArray } from "https://deno.land/x/assertion@$VERSION/assert_array.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertArray([]));
assertThrows(() => assertArray({}));

assertAsyncIterable

badge

Assert the input is AsyncIterable.

import { assertAsyncIterable } from "https://deno.land/x/assertion@$VERSION/assert_async_iterable.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(
  assertAsyncIterable({
    async *[Symbol.asyncIterator]() {
      yield "hello";
    },
  }),
);
assertThrows(() => assertAsyncIterable(() => {}));

assertBigint

badge

Assert the input is bigint.

import { assertBigint } from "https://deno.land/x/assertion@$VERSION/assert_bigint.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertBigint(1000n));
assertThrows(() => assertBigint(undefined));

assertBoolean

badge

Assert the input is boolean.

import { assertBoolean } from "https://deno.land/x/assertion@$VERSION/assert_boolean.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertBoolean(true));
assertThrows(() => assertBoolean(null));

assertDate

badge

Assert the input is Date.

import { assertDate } from "https://deno.land/x/assertion@$VERSION/assert_date.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertDate(new Date()));
assertThrows(() => assertDate({}));

assertError

badge

Assert the input is Error.

import { assertError } from "https://deno.land/x/assertion@$VERSION/assert_error.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertError(Error()));
assertFalse(assertError(new SyntaxError()));
assertThrows(() => assertError(new Date()));

assertFunction

badge

Assert the input is Function.

import { assertFunction } from "https://deno.land/x/assertion@$VERSION/assert_function.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertFunction(() => {}));
assertThrows(() => assertFunction({}));

assertIterable

badge

Assert the input is Iterable.

import { assertIterable } from "https://deno.land/x/assertion@$VERSION/assert_iterable.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertIterable(""));
assertThrows(() => assertIterable({}));

assertNonNullable

badge

Assert the input is not null or undefined.

import { assertNonNullable } from "https://deno.land/x/assertion@$VERSION/assert_non_nullable.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertNonNullable(""));
assertThrows(() => assertNonNullable(null));
assertThrows(() => assertNonNullable(undefined));

assertNull

badge

Assert the input is null.

import { assertNull } from "https://deno.land/x/assertion@$VERSION/assert_null.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertNull(null));
assertThrows(() => assertNull(undefined));

assertNullable

badge

Assert the input is null or undefined.

import { assertNullable } from "https://deno.land/x/assertion@$VERSION/assert_nullable.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertNullable(null));
assertFalse(assertNullable(undefined));
assertThrows(() => assertNullable({}));

assertNumber

badge

Assert the input is number.

import { assertNumber } from "https://deno.land/x/assertion@$VERSION/assert_number.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertNumber(1000));
assertThrows(() => assertNumber("hello world"));

assertObject

badge

Assert the input is object.

import { assertObject } from "https://deno.land/x/assertion@$VERSION/assert_object.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertObject({}));
assertThrows(() => assertObject(null));

assertPrimitive

badge

Assert the input is Primitive.

import { assertPrimitive } from "https://deno.land/x/assertion@$VERSION/assert_primitive.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertPrimitive(true));
assertThrows(() => assertPrimitive({}));
type Primitive =
  | number
  | string
  | boolean
  | bigint
  | undefined
  | null
  | symbol;

assertPromise

badge

Assert the input is Promise.

import { assertPromise } from "https://deno.land/x/assertion@$VERSION/assert_promise.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertPromise(Promise.resolve()));
assertThrows(() => assertPromise({}));

assertRegExp

badge

Assert the input is RegExp.

import { assertRegExp } from "https://deno.land/x/assertion@$VERSION/assert_reg_exp.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertRegExp(new RegExp("")));
assertThrows(() => assertRegExp({}));

assertString

badge

Assert the input is string.

import { assertString } from "https://deno.land/x/assertion@$VERSION/assert_string.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertString("hello world"));
assertThrows(() => assertString(1000));

assertSymbol

badge

Assert the input is symbol.

import { assertSymbol } from "https://deno.land/x/assertion@$VERSION/assert_symbol.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertSymbol(Symbol("symbol")));
assertThrows(() => assertSymbol(null));

assertUndefined

badge

Assert the input is undefined.

import { assertUndefined } from "https://deno.land/x/assertion@$VERSION/assert_undefined.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertUndefined(undefined));
assertThrows(() => assertUndefined(null));

Number subtypes

Assert a subtype of number. All assertion functions must satisfy ⊂ number.

assertEven

badge

Assert the input is even.

import { assertEven } from "https://deno.land/x/assertion@$VERSION/number/assert_even.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertEven(0));
assertThrows(() => assertEven(1));

assertNegativeNumber

badge

Assert the input is negative number.

import { assertNegativeNumber } from "https://deno.land/x/assertion@$VERSION/number/assert_negative_number.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertNegativeNumber(-1));
assertThrows(() => assertNegativeNumber(0));

assertNonNegativeInteger

badge

Assert the input is non-negative integer.

import { assertNonNegativeInteger } from "https://deno.land/x/assertion@$VERSION/number/assert_non_negative_integer.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertNonNegativeInteger(0));
assertFalse(assertNonNegativeInteger(1));
assertThrows(() => assertNonNegativeInteger(-1));

assertNonNegativeNumber

badge

Assert the input is non-negative number. Non-negative number means greater than or equal to zero.

import { assertNonNegativeNumber } from "https://deno.land/x/assertion@$VERSION/number/assert_non_negative_number.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertNonNegativeNumber(0));
assertFalse(assertNonNegativeNumber(1.1));
assertThrows(() => assertNonNegativeNumber(-1));

assertNonPositiveNumber

badge

Assert the input is non-positive number. Non-positive number means less than or equal to zero.

import { assertNonPositiveNumber } from "https://deno.land/x/assertion@$VERSION/number/assert_non_positive_number.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertNonPositiveNumber(0));
assertFalse(assertNonPositiveNumber(-1));
assertThrows(() => assertNonPositiveNumber(1));

assertOdd

badge

Assert the input is odd.

import { assertOdd } from "https://deno.land/x/assertion@$VERSION/number/assert_odd.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertOdd(1));
assertThrows(() => assertOdd(0));

assertPositiveNumber

badge

Assert the input is positive number.

import { assertPositiveNumber } from "https://deno.land/x/assertion@$VERSION/number/assert_positive_number.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertPositiveNumber(1));
assertThrows(() => assertPositiveNumber(0));

assertUnitInterval

badge

Assert the input is unit interval. The unit interval means to the interval between 0 and 1 on the real number line.

import { assertUnitInterval } from "https://deno.land/x/assertion@$VERSION/number/assert_unit_interval.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertUnitInterval(0));
assertFalse(assertUnitInterval(1.0));
assertThrows(() => assertUnitInterval(-1));

Iterable subtypes

Assert a subtype of Iterable. All assertion functions must satisfy ⊂ Iterable<unknown>.

assertEmpty

badge

Assert the input is empty.

import { assertEmpty } from "https://deno.land/x/assertion@$VERSION/iterable/assert_empty.ts";
import { assertFalse } from "https://deno.land/std/testing/asserts.ts";

assertFalse(assertEmpty(""));
assertFalse(assertEmpty([]));
assertFalse(assertEmpty(new Set()));

string:

If the input is a string, it has a "" assertion.

array:

If the input is a array, it has a [] assertion.

assertNotEmpty

badge

Assert the input is not empty.

import { assertNotEmpty } from "https://deno.land/x/assertion@$VERSION/iterable/assert_not_empty.ts";
import { assertFalse } from "https://deno.land/std/testing/asserts.ts";

assertFalse(assertNotEmpty("a"));
assertFalse(assertNotEmpty([0, 1]));

array:

If the input is a T[], it has a [T, ...T[]] assertion.

assertSingle

badge

Assert the input is single element.

import { assertSingle } from "https://deno.land/x/assertion@$VERSION/iterable/assert_single.ts";
import {
  assert,
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";

assertFalse(assertSingle("a"));
assertFalse(assertSingle([0]));
assertThrows(() => assertSingle([0, 1, 2]));

array:

If the input is a T[], it has a [T] assertion.

Date subtypes

Validates a subtype of Date. All validate functions must satisfy ⊂ Date.

assertValidDate

badge

Assert the input is valid Date.

import { assertValidDate } from "https://deno.land/x/assertion@$VERSION/date/assert_valid_date.ts";
import {
  assertFalse,
  assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
assertFalse(assertValidDate(new Date("2000/1/1")));
assertThrows(() => assertValidDate(new Date("invalid")));

Bundle size

Bundle size is not exact. It is only a guide.

Usually, the actual bundle size is smaller than the indicated value.

Where is mod?

There is no single entry point such as mod.

This prevents the inclusion of many unnecessary modules.

License

Copyright © 2023-present Tomoki Miyauchi.

Released under the MIT license