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

tsertion

v1.0.0

Published

Koa middleware for handling errors.

Downloads

5

Readme

Tsertion

Build Status

Low level type guards and assertions for typescript.

Install

npm install tsertion

Usage

import { assertString, getObject, getArray, getNumber } from 'tsertion';

const a: unknown = 'foo';
const b: unknown = null;
const c: unknown = [1, 2, 3];

assertString(a, 'a');

console.log(a.trim());
console.log(getObject(b, 'b').foo); // throws a runtime type error
console.log(
  getArray(c, 'c')
    .map(value => getNumber(value))
    .reduce((a, b) => a + b, 0)
);

API

string

assertString(value: unknown, name?: string): asserts value is string

Throws if value isn't a string.

assertStringOptional(value: unknown, name?: string): asserts value is (string | undefined)

Throws if value isn't a string or undefined.

assertStringNullable(value: unknown, name?: string): asserts value is (string | null)

Throws if value isn't a string or null.

assertStringOptionalNullable(value: unknown, name?: string): asserts value is (string | undefined | null)

Throws if value isn't a string or undefined. or null.

getString(value: unknown, name?: string): string

Throws if value isn't a string. Otherwise returns value.

getStringOptional(value: unknown, name?: string): string | undefined

Throws if value isn't a string or undefined. Otherwise returns value.

getStringNullable(value: unknown, name?: string): string | null

Throws if value isn't a string or null. Otherwise returns value.

getStringOptionalNullable(value: unknown, name?: string): string | undefined | null

Throws if value isn't a string or undefined. Otherwise returns value.

number

assertNumber(value: unknown, name?: string): asserts value is number

Throws if value isn't a number.

assertNumberOptional(value: unknown, name?: string): asserts value is (number | undefined)

Throws if value isn't a number or undefined.

assertNumberNullable(value: unknown, name?: string): asserts value is (number | null)

Throws if value isn't a number or null.

assertNumberOptionalNullable(value: unknown, name?: string): asserts value is (number | undefined | null)

Throws if value isn't a number or undefined. or null.

getNumber(value: unknown, name?: string): number

Throws if value isn't a number. Otherwise returns value.

getNumberOptional(value: unknown, name?: string): number | undefined

Throws if value isn't a number or undefined. Otherwise returns value.

getNumberNullable(value: unknown, name?: string): number | null

Throws if value isn't a number or null. Otherwise returns value.

getNumberOptionalNullable(value: unknown, name?: string): number | undefined | null

Throws if value isn't a number or undefined. Otherwise returns value.

boolean

assertBoolean(value: unknown, name?: string): asserts value is boolean

Throws if value isn't a boolean.

assertBooleanOptional(value: unknown, name?: string): asserts value is (boolean | undefined)

Throws if value isn't a boolean or undefined.

assertBooleanNullable(value: unknown, name?: string): asserts value is (boolean | null)

Throws if value isn't a boolean or null.

assertBooleanOptionalNullable(value: unknown, name?: string): asserts value is (boolean | undefined | null)

Throws if value isn't a boolean or undefined. or null.

getBoolean(value: unknown, name?: string): boolean

Throws if value isn't a boolean. Otherwise returns value.

getBooleanOptional(value: unknown, name?: string): boolean | undefined

Throws if value isn't a boolean or undefined. Otherwise returns value.

getBooleanNullable(value: unknown, name?: string): boolean | null

Throws if value isn't a boolean or null. Otherwise returns value.

getBooleanOptionalNullable(value: unknown, name?: string): boolean | undefined | null

Throws if value isn't a boolean or undefined. Otherwise returns value.

object

assertObject(value: unknown, name?: string): asserts value is Record<any, unknown>

Throws if value isn't an object.

assertObjectOptional(value: unknown, name?: string): asserts value is (Record<any, unknown> | undefined)

Throws if value isn't an object or undefined.

assertObjectNullable(value: unknown, name?: string): asserts value is (Record<any, unknown> | null)

Throws if value isn't an object or null.

assertObjectOptionalNullable(value: unknown, name?: string): asserts value is (Record<any, unknown> | undefined | null)

Throws if value isn't an object or undefined. or null.

getObject(value: unknown, name?: string): Record<any, unknown>

Throws if value isn't an object. Otherwise returns value.

getObjectOptional(value: unknown, name?: string): Record<any, unknown> | undefined

Throws if value isn't an object or undefined. Otherwise returns value.

getObjectNullable(value: unknown, name?: string): Record<any, unknown> | null

Throws if value isn't an object or null. Otherwise returns value.

getObjectOptionalNullable(value: unknown, name?: string): Record<any, unknown> | undefined | null

Throws if value isn't an object or undefined. Otherwise returns value.

array

assertArray(value: unknown, name?: string): asserts value is unknown[]

Throws if value isn't an array.

assertArrayOptional(value: unknown, name?: string): asserts value is (unknown[] | undefined)

Throws if value isn't an array or undefined.

assertArrayNullable(value: unknown, name?: string): asserts value is (unknown[] | null)

Throws if value isn't an array or null.

assertArrayOptionalNullable(value: unknown, name?: string): asserts value is (unknown[] | undefined | null)

Throws if value isn't an array or undefined. or null.

getArray(value: unknown, name?: string): unknown[]

Throws if value isn't an array. Otherwise returns value.

getArrayOptional(value: unknown, name?: string): unknown[] | undefined

Throws if value isn't an array or undefined. Otherwise returns value.

getArrayNullable(value: unknown, name?: string): unknown[] | null

Throws if value isn't an array or null. Otherwise returns value.

getArrayOptionalNullable(value: unknown, name?: string): unknown | undefined | null

Throws if value isn't an array or undefined. Otherwise returns value.