Home

Awesome

check-types.js

Package status Build status License

A little JavaScript library for asserting types and values.

Why would I want that?

Writing explicit conditions in your functions to check arguments and throw exceptions is a task that swiftly becomes tiresome and adds complexity to your codebase.

The purpose of check-types.js is to remove this burden from JavaScript application developers in an efficient and robust manner, abstracted by a simple API.

How little is it?

21 kb unminified with comments, 6.1 kb minified, 2.2 kb minified + gzipped.

How do I install it?

Via npm:

npm i check-types --save

Or if you just want the git repo:

git clone git@github.com:philbooth/check-types.js.git

If you're into other package managers, it is also available from Bower, Component and Jam.

How do I use it?

Loading the library

If you are running in Node.js, Browserify or another CommonJS-style environment, you can require check-types like so:

var check = require('check-types');

It also the supports the AMD-style format preferred by Require.js.

If you are including check-types.js with an HTML <script> tag, or neither of the above environments are detected, it will export the interface globally as check.

Calling the exported functions

Once you've loaded the library in your application, a whole bunch of functions are available to call.

Most of the functions are predicates, which can be executed in a number of different contexts:

Additionally, there are some batch operations that allow you to apply different predicates to each value in an array or object. These are implemented by check.apply, check.map, check.any and check.all.

General predicates

String predicates

Number predicates

Boolean predicates

Object predicates

Array predicates

Date predicates

Function predicates

Modifiers

Batch operations

Some examples

check.even(3);
// Returns false
check.not.even(3);
// Returns true
check.maybe.even(null);
// Returns true
check.assert.like({ foo: 'bar' }, { baz: 'qux' });
// Throws `new TypeError('Invalid type')`
check.assert.not.like({ foo: 'bar' }, { baz: 'qux' });
// Doesn't throw, returns `{ foo: 'bar' }`
check.assert.maybe.like(undefined, { foo: 'bar' });
// Doesn't throw, returns `undefined`
check.assert(myFunction(), 'Something went wrong', CustomError);
// Throws `new CustomError('Something went wrong')` if myFunction returns `false`
check.apply([ 'foo', 'bar', '' ], check.nonEmptyString);
// Returns [ true, true, false ]
check.map({
    foo: 2,
    bar: { baz: 'qux' }
}, {
    foo: check.odd,
    bar: { baz: check.nonEmptyString }
});
// Returns { foo: false, bar: { baz: true } }
check.all(
    check.map(
        { foo: 0, bar: '' },
        { foo: check.number, bar: check.string }
    )
);
// Returns true
check.any(
    check.apply(
        [ 1, 2, 3, '' ],
        check.string
    )
);
// Returns true

Are there TypeScript definitions?

Yes!

Thanks to @idchlife, type definitions were added to DefinitelyTyped. You can add them to your project via npm:

npm i @types/check-types --save-dev

Where can I use it?

As of version 2.0, this library no longer supports ES3. That means you can't use it in IE 7 or 8.

Everywhere else should be fine.

If those versions of IE are important to you, worry not! The 1.x versions all support old IE and any future 1.x versions will adhere to that too.

See the releases for more information.

What changed from 6.x to 7.x?

Breaking changes were made to the API in version 7.0.0.

Specifically, the instance predicate was renamed to instanceStrict and the builtIn and userDefined predicates were combined to form a new instance predicate.

See the history for more details.

What changed from 5.x to 6.x?

Breaking changes were made to the API in version 6.0.0.

Specifically, the either modifier was removed. Instead, calling code can use the any function, or simply express the boolean logic in JS.

See the history for more details.

What changed from 4.x to 5.x?

Breaking changes were made to the API in version 5.0.0.

Specifically, the predicates isMap and error were removed in favour of the new predicate builtIn, which can be used to test for all built-in objects.

See the history for more details.

What changed from 3.x to 4.x?

Breaking changes were made to the API in version 4.0.0.

Specifically, the predicate unemptyString was renamed to nonEmptyString and the predicate error was changed to support derived Error objects.

See the history for more details.

What changed from 2.x to 3.x?

Breaking changes were made to the API in version 3.0.0.

Specifically, the predicate length was renamed to hasLength and the predicate webUrl was removed.

See the history for more details.

What changed from 1.x to 2.x?

Breaking changes were made to the API in version 2.0.0.

Specifically:

See the history for more details.

What changed from 0.x to 1.x?

Breaking changes were made to the API in version 1.0.0.

Specifically, all of the predicates were renamed from check.isXxxx to check.xxx and all of the verifiers were renamed from check.verifyXxxx to check.verify.xxx.

See the history for more details.

How do I set up the build environment?

The build environment relies on Node.js, NPM, JSHint, Mocha, Chai, UglifyJS and please-release-me. Assuming that you already have Node.js and NPM set up, you just need to run npm install to install all of the dependencies as listed in package.json.

The unit tests are in test/check-types.js. You can run them with the command npm test. To run the tests in a web browser, open test/check-types.html.

What license is it released under?

MIT