A tiny JavaScript library for checking arguments and throwing exceptions.
- Why would I want that?
- How tiny is it?
- How do I install it?
- How do I use it?
- What changed from 0.x to 1.x?
- How do I set up the build environment?
- What license is it released under?
Writing explicit conditions in your functions for checking arguments and throwing 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.
11.9 kb unminified with comments, 2.7 kb minified, 1 kb minified + gzipped.
Any of the following will do:
npm install check-types
jam install check-types
bower install check-types
component install philbooth/check-types.js
git clone git@github.com:philbooth/check-types.js.git
If you are running in
Node.js,
Browserify
or another CommonJS-style
environment,
you can require
check-types.js like so:
var check = require('check-types');
It also the supports the AMD-style format preferred by Require.js:
require.config({
paths: {
check: 'check-types.js/src/check-types'
}
});
require([ 'check' ], function (check) {
});
If you are
including check-types.js
with an HTML <script>
tag,
or neither of the above environments
are detected,
check-types.js will just export its interface globally
as check
.
Once you have loaded the library in your application, a whole bunch of functions are available to call.
For the most part, the exported functions are broadly split into four types.
-
check.xxx(thing)
: These functions are predicates, returning true or false depending on the type and value ofthing
. -
check.maybe.xxx(thing)
: The maybe modifier returnstrue
ifthing
isnull
orundefined
, otherwise it returns the result of the equivalent predicate. -
check.not.xxx(thing)
: The not modifier negates a predicate, returningtrue
if the predicate returnsfalse
andfalse
if the predicate returnstrue
. -
check.verify.xxx(thing, message)
: The verify modifier calls the equivalent predicate and throws anError
if the result isfalse
. It can also be applied to maybe and not modifiers using the formcheck.verify.maybe.xxx(thing, message)
orcheck.verify.not.xxx(thing, message)
respectively.
Additionally, there are some batch operations
that allow you to test maps
of many predicates at once.
These are implemented by
check.map
,
check.any
and
check.every
.
-
check.string(thing)
: Returnstrue
ifthing
is a string,false
otherwise. -
check.unemptyString(thing)
: Returnstrue
ifthing
is a non-empty string,false
otherwise. -
check.webUrl(thing)
: Returnstrue
ifthing
is an HTTP or HTTPS URL,false
otherwise. -
check.gitUrl(thing)
: Returnstrue
ifthing
is a git+ssh, git+http or git+https URL,false
otherwise. -
check.length(thing, value)
: Returnstrue
ifthing
has a length property that equalsvalue
,false
otherwise.
-
check.number(thing)
: Returnstrue
ifthing
is a number,false
otherwise. In this case,NaN
is not considered a number. -
check.positiveNumber(thing)
: Returnstrue
ifthing
is a number greater than zero,false
otherwise. -
check.negativeNumber(thing)
: Returnstrue
ifthing
is a number less than zero,false
otherwise. -
check.oddNumber(thing)
: Returnstrue
ifthing
is an odd number,false
otherwise. -
check.evenNumber(thing)
: Returnstrue
ifthing
is an even number,false
otherwise.
check.fn(thing)
: Returnstrue
ifthing
is a function,false
otherwise.
-
check.array(thing)
: Returnstrue
ifthing
is an array,false
otherwise. -
check.length(thing, value)
: Returnstrue
ifthing
has a length property that equalsvalue
,false
otherwise.
check.date(thing)
: Returnstrue
ifthing
is a date,false
otherwise.
-
check.object(thing)
: Returnstrue
ifthing
is a non-null, non-array, non-date object,false
otherwise. -
check.emptyObject(thing)
: Returnstrue
ifthing
is an empty object,false
otherwise. -
check.instance(thing, prototype)
: Returnstrue
ifthing
is an instance ofprototype
,false
otherwise. -
check.like(thing, duck)
: Duck-typing checker. Returnstrue
ifthing
has all of the properties ofduck
,false
otherwise. If either argument is not an object, an exception is thrown.
-
check.maybe.xxx(...)
: Returnstrue
ifthing
isnull
orundefined
, otherwise it propagates the return value from its predicate. -
check.verify.xxx(...)
/check.verify.maybe.xxx(...)
: Throws anError
if the predicate returns false. The last argument is an optional message to be set on theError
instance.
-
check.map(things, functions)
: Maps each predicate from thefunctions
object to the corresponding value fromthings
, returning the hash of results. Similar tolike
but using functions instead of values. Supports nested objects. -
check.every(predicateResults)
: Returnstrue
if all properties of thepredicateResults
object aretrue
,false
otherwise. -
check.any(predicateResults)
: Returnstrue
is any property of thepredicateResults
object istrue
,false
otherwise.
check.object(0);
// Returns false
check.maybe.object(null);
// Returns true
check.not.object(0);
// Returns true
check.verify.like({}, { foo: 'bar' }, 'Invalid object');
// Throws new Error('Invalid object')
check.verify.maybe.like(undefined, { foo: 'bar' }, 'Invalid object');
// Doesn't throw
check.verify.not.like({}, { foo: 'bar' }, 'Invalid object');
// Doesn't throw
check.map({
foo: 2,
bar: {
baz: 'qux'
}
}, {
foo: check.oddNumber,
bar: {
baz: check.unemptyString
}
});
// Returns { foo: false, bar: { baz: true } }
check.every(
check.map({
foo: 0,
bar: ''
}, {
foo: check.number,
bar: check.unemptyString
})
);
// Returns false
check.any(
check.map({
foo: 0,
bar: ''
}, {
foo: check.number,
bar: check.unemptyString
})
);
// Returns true
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.
The build environment relies on
Node.js,
NPM,
JSHint,
Mocha,
Chai and
UglifyJS.
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
.