npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

assert-ts

v0.3.4

Published

Invariant and non-null/undefined assertion check with type guards

Downloads

17,945

Readme

assert-ts

Invariant and non-null/undefined assertions, both

  • strict, i.e. logs error and throws exception, and
  • soft, i.e. just logs a warning.

Both strict and soft will narrow type, i.e. eliminate null or undefined.

Introduction

The purpose of this library is to make assumptions explicit, rather than just a comment or even worse just a thought while writing your code. This applies both to assumptions about conditions to be met or values not being null/undefined.

Installation

To install the library into your project, run yarn or npm:

yarn add assert-ts

or

npm i assert-ts

Examples

Assert condition, strict

import assert from 'assert-ts';

function transfer(fromId: string, toId: string, amount: number) {
  // Throws error if not true
  assert(amount > 0);
  ...
}

Assert condition, soft

import assert from 'assert-ts';

function transfer(fromId: string, toId: string, amount: number) {
  // Logs warning if false
  if (assert.soft(amount > 0)) {
    ...
  }
}

Assert condition with more context info

To make it easier to find the cause of an assertion failure, you can provide more information, i.e. a custom message and any relevant properties.

import assert from 'assert-ts';

function transfer(fromId: string, toId: string, amount: number) {
  // Custom message and properties will be formatted into error message
  assert(amount > 0, "Cannot transfer 0 or negative amounts", { fromId, toId, ammount });
  ...
}

Assert non-null/undefined

import assert from 'assert-ts';

function findAccount(id): Account | undefined { ... }

function transfer(fromnId: string, toId: string, amount: number) {
  ...

  // Throws error if findAccount returns undefined
  const fromAccount = assert(findAccount(fromId), "From account does not exist", { fromId});

  // Type restriction: when a non-null/undefined assertion succeeds,
  // type is restricted, e.g. to Account. Hence, no need for further testing of undefined/null
  fromAccount.amount -= amount;

  ...
}

Assert non-null/undefined, soft

import assert from 'assert-ts';


function notify(person?: Person) {
  ...
  // Logs warning if person is undefined
  if (assert.soft(person, 'Person should be provided')) {
    // person narrowed to Person
    sendSms(person.mobile);
  }
}

API

assert has two signatures

Assert condition

Checks that a condition is true. If not, an error is thrown. By default, any message or properties provided will be formatted as part of the error's message. See below for custom configuration.

function assert(
  condition: boolean,
  message?: string,
  props?: object | (() => object),
): asserts condition;

Assert non-null/undefined

Checks that a value is not null or undefined. If null or undefined, an error is thrown. When successful, the returned value's type is restricted to the expected type.

function assert<T>(
  value: T | undefined | null,
  message?: string,
  props?: object | (() => object),
): T;

assert.soft has two signatures

Check condition

Checks that a condition is true. If not, logs a warning. By default, any message or properties provided will be formatted as part of the warning message. See below for custom configuration.

function soft(
  condition: boolean,
  message?: string,
  props?: object | (() => object),
): condition is true;

Check non-null/undefined

Checks that a value is not null or undefined. If null or undefined, a warning is logged. When successful, the value's type is narrowed to the expected type.

function soft<T>(
  value: T | undefined | null,
  message?: string,
  props?: object | (() => object),
): value is T;

Configuration

The default configuration throws an Error with a message saying whether it was a condition or null/undefined check that failed and any custom message or properties formatted as part of the message.

Use configureAssert to customize this, providing an AssertConfiguration object with any of the following properties:

| Property | Description | | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | | formatter | To do custom formatting of error message (failureType: FailureType, message?: string, props?: object) => string | | errorCreator | To create custom error objects (failureType: FailureType, message?: string, props?: object) => Error | | errorReporter | To do custom reporting of assertion failures, e.g. report to backend (failureType: FailureType, error: Error, message?: string, props?: object) => void | | warningReporter | To do custom reporting of soft assertion failures, e.g. report to backend (failureType: FailureType, message?: string, props?: object) => void |

Contributors

Bjørn Egil Hansen (@bjornegil)

Sponsors

Fram X - a cross platform app company from Norway.