@mmutils/assert
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

Typed assert utility functions

Assert and type guard functions that utilize TypeScript's type predicates and asserts to narrow down types in Control Flow Analysis at compile time and add fail safe at runtime. Makes refactoring easy and keeps the code clean, no clutter conditions. Supports all primitive types and just a few more to keep the package clean and light.

Highlights

Comes in two flavours. assertIs[Type] utilizing asserts and is[Type] utilizing type guards. Under the hood assertIs[Type] uses is[Type].

Assert flavour:

const x = (param?: string) => {
  param; // param is string | undefined
  assertIsNonNullish(param); // throws and error if param is undefined or null
  param; // param is string for the rest of the function body
};

Condition flavour:

const x = (param?: string | string[]) => {
  param; // param is string | string[] | undefined
  if (isNonNullish(param)) {
    param; // param is string | string[] in this code block
  } else if (isArray(param)) {
    param; // param is string[] in this code block
  } else if (isString(param)) {
    param; // param is string in this code block
  }
};

Installation

Install the package

npm install @mmutils/assert
# or
yarn add @mmutils/assert

Load as module

import {
  assert,
  assertIsArray,
  assertIsBigInt,
  assertIsBoolean,
  assertIsFunction,
  assertIsNonNullish,
  assertIsNumber,
  assertIsObject,
  assertIsString,
  assertIsSymbol,
  isArray,
  isBigInt,
  isBoolean,
  isFunction,
  isNonNullish,
  isNumber,
  isObject,
  isString,
  isSymbol,
} from '@mmutils/assert';
// or
import {
  assert,
  assertIsArray,
  assertIsBigInt,
  assertIsBoolean,
  assertIsFunction,
  assertIsNonNullish,
  assertIsNumber,
  assertIsObject,
  assertIsString,
  assertIsSymbol,
  isArray,
  isBigInt,
  isBoolean,
  isFunction,
  isNonNullish,
  isNumber,
  isObject,
  isString,
  isSymbol,
} from '@mmutils/assert/esm/';

Load as commonjs

const {
  assert,
  assertIsArray,
  assertIsBigInt,
  assertIsBoolean,
  assertIsFunction,
  assertIsNonNullish,
  assertIsNumber,
  assertIsObject,
  assertIsString,
  assertIsSymbol,
  isArray,
  isBigInt,
  isBoolean,
  isFunction,
  isNonNullish,
  isNumber,
  isObject,
  isString,
  isSymbol,
} = require('@mmutils/assert');
// or
const {
  assert,
  assertIsArray,
  assertIsBigInt,
  assertIsBoolean,
  assertIsFunction,
  assertIsNonNullish,
  assertIsNumber,
  assertIsObject,
  assertIsString,
  assertIsSymbol,
  isArray,
  isBigInt,
  isBoolean,
  isFunction,
  isNonNullish,
  isNumber,
  isObject,
  isString,
  isSymbol,
} = require('@mmutils/assert/cjs/');

Changelog

v0.0.3

Removed redundant files.

v0.0.2

Documentation changes.

v0.0.1

Initial release.

Usage

Check the corresponding docs for reference guide.

License

This project is licensed under the MIT License.

Package Sidebar

Install

npm i @mmutils/assert

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

64 kB

Total Files

35

Last publish

Collaborators

  • mastermind_0xff