Announcing Powercheck, a non-meteor equivalent for check and Match


#1

One of the powerful tools of Meteor is the check() library. Wherever you want, you can write a single rule to check the type or instance of a variable and let your application throw an exception when the check fails. For example:

function MyFunction(someArg, callback) {
    check(someArg1, String);
    check(callback, Match.Optional(Function));

    //
}

Although Meteor is currently (since 1.3) splitting it’s core into multiple endpoints, it’s not yet possible to use this powerful too apart from the Meteor core, e.g. in a separate JavaScript application.

#Powercheck
Announcing the lightweight (1.8 kB) equivalent Powercheck, published on NPM. It’s use is very similar to Meteor’s check library we’re all used to. For consistency throughout your projects, you can even use it in your Meteor applications (especially with the new npm module system in Meteor 1.3).

npm install powercheck --save

##Usage

import check from 'powercheck';
 
check('foo', String);
    // -> true 
 
check(new Date(), Date);
    // -> true 
 
check(new SomeConstructor(), SomeConstructor);
    // -> true 
 
check('foo', check.equals('bar'));
    // -> false 
 
check(undefined, check.optional(String));
    // -> true 
 
check('foo', check.validate((value) => {
    return ['foo', 'bar'].indexOf(value) > -1;
}));
    // -> true 
 
check.throw('foo', Number);
    // -> throws an exception (like Meteor's check() function)

check.throw('foo', Number, 'My Custom Error Message');
    // -> throws an exception with a custom error message

Enjoy and happy coding :wink:


#2

That’s awesome, I like Meteor’s check feature a lot too, now we can use it anywhere!