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

powercheck

v4.1.3

Published

Straightforward, zero-dependencies and powerful utility to check types, instances or values

Downloads

154

Readme

powercheck

npm version

Straightforward, zero-dependencies and powerful utility to check types, instances or values. Takes away the dirt, lets you write more solid code and drastically improves code readability.

npm install powercheck --save

Usage

Basic

import is, { Throw as check } from 'powercheck';

is('foo', String);
    // -> true

is('foo', Number);
    // -> false

check('foo', String);
    // -> undefined

check('foo', Number);
    // throws an exception

Optional value

is('foo', is.optional(String));
    // -> true

is('bar', is.optional(is.equals('bar')));
    // -> true

Or: import "optional" using ES6 modules

import is, { optional } from 'powercheck';

is('foo', optional(String));
    // -> true

Instance checking

is(new Date(), Date);
    // -> true

is(new SomeConstructor(), SomeConstructor);
    // -> true

Equality checking

is('foo', is.equals('bar'));
    // -> false

OneOf-checking

is('foo', is.oneOf([Number, Boolean]));
    // -> false

is('foo', is.oneOf([is.equals(2), String]));
    // -> true

is('foo', is.oneOf([
    is.equals('foo'),
    is.equals('bar')
]));
    // -> true

is('foo', is.oneOf(['foo', 'bar'].map(is.equals)));
    // -> true

Every-checking

is('foo', is.every([String, is.equals('foo')]));
    // -> true

is(123, is.every([Number, is.validate(n => n < 100)]));
    // -> false

N.B.: validations will be executed in order. When a validation fails, remaining validations won't be executed. So, in is.validate() functions (custom validations), you can rely on the previous validations.

Validation function

is('foo', is.validate((value) => {
    return ['foo', 'bar'].indexOf(value) > -1;
}));
    // -> true
import { isEmail } from 'validator';

is('[email protected]', is.validate(isEmail));
    // -> true

Array literal

is(['foo', 'bar'], [String]);
    // -> true

is(['foo', 400], [String]);
    // -> false

N.B.: it's recursive

is([['foo', 'test'], ['bar']], [[String]]);
    // -> true

Object literal

is({
    foo: 'bar'
}, {
    foo: String
});
    // -> true

is({
    foo: 'bar',
    baz: 'kopz'
}, {
    foo: String,
    baz: Number
});
    // -> false

N.B.: extra properies won't be accepted

is({
    foo: 'bar',
    baz: 'kopz',
    extra: 'boo'
}, {
    foo: String,
    baz: String
});
    // -> false

N.B.: mising properies won't be accepted

is({
    foo: 'bar',
    baz: 'kopz',
}, {
    foo: String,
    baz: String,
    extra: String
});
    // -> false

Throw exceptions instead

import is, { Throw as check } from 'powercheck';

is('foo', Number);
    // -> false (boolean)

check('foo', Number);
    // -> throws an exception

check('foo', Number, new Error('Failed'));
    // -> throws a custom error

check('foo', Number, (value, error) => {
    return new Error('Type ' + error.got + ' is invalid. Should be ' + error.expectedType + '.');
});
    // -> throws a custom error with extra information

check('foo', String);
    // -> undefined (no exception has been thrown, nothing is being returned either)

check(undefined, is.optional(Number));
    // -> undefined (no exception has been thrown, nothing is being returned either)

API

is(value, validator)

is() returns a boolean and check() throws an exception.

Key | Value --- | ---- value | Anything you want to check the type, instance and/or value for validator | String, Number, Object, Array, Function, Symbol (ES6), SomeConstructor, is.optional(<validator>), is.validate((value) => trueOrFalse), is.equals(compareValue), is.oneOf([validators]), [<validator>], {key1: <validator>, ...}

Questions

If you have questions, you can ask them on StackOverflow mentioning me @jessedvrs.