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

@public-cloud-group/is

v1.0.2

Published

This library provides several predicates and assertion functions as well as utilities to compose predicates and create your own ones.

Downloads

271

Readme

@public-cloud-group/is

This library provides several predicates and assertion functions as well as utilities to compose predicates and create your own ones.

Why was this created?

There already exists a multitude of assertion libraries within the node ecosystem. However, most of them fall in (at least) one of three pitfalls:

  1. They are not (or hardly) extendable and instead try to cover everything you might want to assert (e.g. jasmine)
  2. They are completely bare-bones and require lots of work just to be useful in the first place (e.g. Node.JS' "assert" module)
  3. They are not usable in every environment (e.g. AVA)

@public-cloud-group/is tries to provide assertions without falling in these pitfalls.

How is that achieved?

Utilizing features of functional programming and typescripts predicate functions allows creating functions that serve exactly one purpose and that are easily composable.

How to use it?

To use a predicate you first have to instantiate it and then call it. In the field this looks roughly like this:

import {assert, createPredicate, is, isUndefined, isNull, or} from "@public-cloud-group/is";

// "is" is predicate factory. So we have to call it first to get a predicate back
const isFour = is(4 as const);

// when creating a predicate, you must first give a human-readable string-representation of the type and then the actual
// predicate function
const isFive = createPredicate("5", (it: unknown): it is 5 => it === 5);

// When composing make sure to pass the predicates as parameters and not accidentally call them. You may, however, call
// predicate factories to create the predicates in-place.
const isFourOrHelloWorldOrUndefinedOrNull = or(isFour, is("Hello, World!" as const), isUndefined, isNull);

// You can also pass a predicate into createPredicate (mostly useful for changing the human-readable type description
// which is used in error messages)
const isMyValue = createPredicate("my-value", isFourOrHelloWorldOrUndefinedOrNull);

// A value which we do not know anything about
let unknownValue: unknown;

// When used for type-checking
if (isMyValue(unknownValue)) {
    // here unknownValue is auto-narrowed to null|undefined|4|"Hello, World!"
}

// here unknownValue is unknown again

assert(unknownValue, isMyValue);

// here unknownValue is again auto-narrowed to null|undefined|4|"Hello, World!"

let unknownValue2: unknown = 4;

// The next line produces a runtime Error: expected 4 to be 5
assert(unknownValue2, isFive);

// unknownValue2 is auto-narrowed to 5

// The next line produces a compiler Error: 5 is not assignable to 4
assert(unknownValue2, isFour);

When to use it?

This library is mostly (but not exclusively) designed for two use-cases:

  1. When you have a value from an external system (like a parsed json file from the file system or a request/response body of an HTTP request), you cannot trust it to have exactly the type you expect it to have. Here, @public-cloud-group/is can help you check the sanity and integrity of the data.
  2. As an assertion library in tests or at runtime.

What are .metatest.ts files?

Since one of the core features of this library is type checking and it changes typescripts control flow diagram it is necessary to make sure that variables have the correct type when certain functions were called. Checking that is what the metatest files were created for. While it does not make sense to run them they are only meant to be compiled because when they do not compile, something is wrong with the meta-programming that causes the types to not work correctly.

The compilation results of these files will not be included in the distributed package.

Open Tasks