Giter VIP home page Giter VIP logo

powercheck's Introduction

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.

powercheck's People

Contributors

jessedvrs avatar

Stargazers

Rick Lancee avatar Daniel Sam avatar Koen van Gilst avatar Peter Peerdeman avatar  avatar

Watchers

Peter Peerdeman avatar James Cloos avatar  avatar

powercheck's Issues

React-native android can't find Symbol.

I cannot use Powercheck for react-native Android ๐Ÿ˜ข

Importing a polyfill fixes this, but that causes alot of other side-effects. So maybe nog the best workaround..

import 'core-js/fn/symbol';

screenshot_20170505-165044

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.