0.1.2 • Published 8 years ago

ml-check v0.1.2

Weekly downloads
99
License
MIT
Repository
github
Last release
8 years ago

ml-check

An isomorphic, runtime, type checking utility that can be used on the server or in the client. It is used in the reactive framework milojs

Test Coverage

Check is a module for parameters checking extracted from Meteor framework.

It allows to both document and to check parameter types in your function making code both readable and stable.

Usage

var check = milo.check
    , Match = check.Match;

function My(name, obj, cb) {
    // if any of checks fail an error will be thrown
    check(name, String);
    check(obj, Match.ObjectIncluding({ options: Object }));
    check(cb, Function);

    // ... your code
}

See Meteor docs to see how it works

Patterns

All patterns and functions described in Meteor docs work.

Unlike in Meteor, Object pattern matches instance of any class, not only plain object.

In addition to patterns described in Meteor docs the following patterns are implemented

Match.ObjectHash(pattern)

Matches an object where all properties match a given pattern

Match.Subclass(constructor , matchThisClassToo)

Matches a class that is a subclass of a given class. If the second parameter is true, it will also match the class itself.

Without this pattern to check if MySubclass is a subclass of MyClass you would have to use

check(MySubclass, Match.Where(function() {
   return MySubclass.prototype instanceof MyClass;
});

Things we explicitly do NOT support:

  • heterogenous arrays