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

@valu/assert

v1.3.3

Published

Small collection of [type predicates](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates) and [assertion functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions).

Downloads

61

Readme

@valu/assert

Small collection of type predicates and assertion functions.

Helps working with possibly undefined types generated from GraphQL queries.

This library has its own concept called "nil" which means null or undefined.

CS 101: Asserting means throwing and crashing if the assertion condition is not met

notNil()

Type predicate for checking nil values

import { notNil } from "@valu/assert";

function fn(text: string | null | undefined) {
    if (notNil(text)) {
        text.toUpperCase(); // ok!
    }
}

which is almost the same as

function fn(text: string | null | undefined) {
    if (text) {
        text.toUpperCase(); // ok!
    }
}

but it will allow an empty string "" since it is not nil.

But its most powerful use case is when used as a type predicate in .filter():

type Node = { value: number } | null | undefined;

const nodes: Nodes[] = [null, undefined, { value: 1 }];

nodes.filter(notNil).map((node) => {
    node.value; // ok!
});

Use when mapping GraphQL node lists!

assertNotNil(value: any, customErrorMessage?: string)

Assertion function for removing nil values.

import { assertNotNil } from "@valu/assert";

function fn(item: { value: number } | null | undefined) {
    assertNotNil(item);

    item.value; // ok!
}

Use when you know the value cannot be nil

assertIs(target: any, value: any customErrorMessage?: string)

Assert that the target is explicitly of the given type

import { assertIs } from "@valu/assert";

function fn(item: { value: number } | boolean | null) {
    assertIs(item, false as const);

    item; // item === false
}

Can be used to assert discriminated unions

type Item =
    | {
          type: "post";
          postTitle: string;
      }
    | {
          type: "page";
          pageTitle: string;
      };

function fn(item: Item) {
    item.postTitle; // Error!
    assertIs(item.type, "post" as const);
    item.postTitle; // ok!
}

function fn2(item: Item) {
    item.pageTitle; // Error!
    assertIs(item.type, "page" as const);
    item.pageTitle; // ok!
}

GraphQL node connections have often types like this.

assertNotBrowser()

Throw if the environment is a web browser. Used assert that server-side code is not accidentally loaded into the browser bundles.

assert(cond: boolean, message: string, offsetStack?: number)

Plain assert function with offsettable stack for better error messages like in:

https://kentcdodds.com/blog/improve-test-error-messages-of-your-abstractions

import { assert } from "@valu/assert";

function assertCustom() {
    assert(true === false, "Fail", 2);
}

assertNotAny()

Assert that the given value is not type of any

import { assertNotAny } from "@valu/assert";

const value: any = "wat";
// Produces type error
assertNotAny(value);

const value = "wat";
// ok because is string
assertNotAny(value);

assertType()

Assert that the given value is not type of any

import { assertType } from "@valu/assert";

const value: string = "wat";
// Produces type error because string is not assignable to number
assertType<number>(value);

const value = "wat";
// ok
assertType<string>(value);