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

@dgillis/assert-types

v0.0.2

Published

assert-types

Downloads

8

Readme

assert-types

A way to perform type assertions with detailed error messages in development but do nothing in production.

Install

With npm do:

npm install @dgillis/assert-types

Examples

var t = require('@dgillis/assert-types');

// Basic types.
t.num(123);  // Returns 123.
t.num("abc");  // Error - expected a number, got: abc
t.int(1.5);  // Error - expected an integer, got: 1.5
t.num(123, {ge: 1000});  // Error - expected a number >= 1000, got: 123
t.str("abc");  // Returns "abc"
t.bool(false);  // Returns false
t.arr([1, 2, 3]);  // Returns [1, 2, 3]

// Shape type - requires a second argument giving an object type
// specification:
t.shape({x: 1, y: false}, {x: 'num', y: 'str'})
// Error - shape did not validate at "y" - expected a string, got: false

// ArrOf - requires a second argument giving a type specification for
// array members:
t.arrOf([1,2,3], 'int');  // Returns [1, 2, 3]
t.arrOf([1, null, 3], 'int');  // Error
t.arrOf([1, null, 3], 'int|nul');  // Returns [1, null, 3]

API

AssertTypes.<typeName>(value, ...typeOptions)

In non-production environment, check if value matches typeName subject to any typeOptions and, if so, return value. Otherwise, throw an Error describing what went wrong.

In production environments, immediately return value without any type checking.

The full list of available types can be viewed here.

Type Specification

Some of the container types (arrOf, plainObjectOf, shape, etc.) require additional arguments specifying nested value types.

AssertTypes.arrOf(value, type)

The arrOf type is used to ensure that value is an array consisting of elements of a certain type. The type parameter should be one of:

  • A type name ("str", "bool", "num", etc.)
  • Multiple type names separated by "|" ("str|nul", "num|str|arr", etc.)
  • An array whose first element is a type name and whose remaining elements are arguments for that type (["int", {ge: 0}], ["arr", {ofLength: 5}], etc.)

AssertTypes.plainObjectOf(value, type)

The plainObjectOf type is used to ensure the value is a plainObject whose values all match type. The type parameter is similar to that in AssertTypes.arrOf().

AssertTypes.shape(value, typeShape)

The shape type is used to ensure that value is a plain object consisting of multiple fields of specific types. The typeShape parameter should be an object of the form {key: type, ...} where type is similar to that used by AssertTypes.arrOf(). Each key: type entry ensures that the key is present in value and that value[key] matches type.

Node

This package is optimized around the type checking being disabled in production use. As such, when process.env.NODE_ENV === 'production', the library consists of nothing but multiple references to the identity function and should compress down to less than a kilobite within a suitable build environment.