JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 17256
  • Score
    100M100P100Q149882F

Assertions for library authors.

Package Exports

  • @brillout/libassert

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@brillout/libassert) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Minimalistic & simple assertions for library authors.

Designed so that you can create all kinds of assertion types.

For example:

import { createError } from "@brillout/libassert";

export { assert, assertUsage, assertWarning };

const libName = "Awesome Library";

function assert(condition: unknown): asserts condition {
  if (condition) {
    return;
  }

  const prefix =
  `[${libName}][Internal Error] Something unexpected happened, `+
  `please open a GitHub issue.`;
  const err = createError({ prefix });

  throw err;
}

function assertUsage(condition: unknown, errorMessage: string): asserts condition {
  if (condition) {
    return;
  }

  const err = createError({
    prefix: `[${libName}][Wrong Usage]`,
    errorMessage,
  });

  throw err;
}

function assertWarning(condition: unknown, errorMessage: string): void {
  if (condition) {
    return;
  }

  const err = createError({
    prefix: `[${libName}][Warning]`,
    errorMessage,
  });

  console.warn(err);
}

The createError(errorMessage) is the same than new Error(${prefix} ${errorMessage}) except that:

  • prefix and errorMessage are forbidden to contain new lines.
  • The stack trace is complete but also cleaned to remove useless information.