Package Exports
- check-types
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (check-types) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
check-types.js
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?
Why would I want that?
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.
How tiny is it?
13.7 kb unminified with comments, 3.1 kb minified, 1.1 kb minified + gzipped.
How do I install it?
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.gitHow 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.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.
Calling the exported functions
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 returnstrueifthingisnullorundefined, otherwise it returns the result of the equivalent predicate.check.not.xxx(thing): The not modifier negates a predicate, returningtrueif the predicate returnsfalseandfalseif the predicate returnstrue.check.verify.xxx(thing, message): The verify modifier calls the equivalent predicate and throws anErrorif 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.
String functions
check.string(thing): Returnstrueifthingis a string,falseotherwise.check.unemptyString(thing): Returnstrueifthingis a non-empty string,falseotherwise.check.webUrl(thing): Returnstrueifthingis an HTTP or HTTPS URL,falseotherwise.check.gitUrl(thing): Returnstrueifthingis a git+ssh, git+http or git+https URL,falseotherwise.check.email(thing): Returnstrueifthingseems like a valid email address,falseotherwise.check.length(thing, value): Returnstrueifthinghas a length property that equalsvalue,falseotherwise.
Number functions
check.number(thing): Returnstrueifthingis a real number,falseotherwise. Note thatNaN,Number.POSITIVE_INFINITYandNumber.NEGATIVE_INFINITYare not real numbers.check.positiveNumber(thing): Returnstrueifthingis a number greater than zero,falseotherwise.check.negativeNumber(thing): Returnstrueifthingis a number less than zero,falseotherwise.check.oddNumber(thing): Returnstrueifthingis an odd number,falseotherwise.check.evenNumber(thing): Returnstrueifthingis an even number,falseotherwise.check.intNumber(thing): Returnstrueifthingis an integer,falseotherwise.check.floatNumber(thing): Returnstrueifthingis a floating-point number,falseotherwise.
Function functions
check.fn(thing): Returnstrueifthingis a function,falseotherwise.
Array functions
check.array(thing): Returnstrueifthingis an array,falseotherwise.check.length(thing, value): Returnstrueifthinghas a length property that equalsvalue,falseotherwise.
Date functions
check.date(thing): Returnstrueifthingis a date,falseotherwise.
Object functions
check.object(thing): Returnstrueifthingis a non-null, non-array, non-date object,falseotherwise.check.emptyObject(thing): Returnstrueifthingis an empty object,falseotherwise.check.instance(thing, prototype): Returnstrueifthingis an instance ofprototype,falseotherwise.check.like(thing, duck): Duck-typing checker. Returnstrueifthinghas all of the properties ofduck,falseotherwise. If either argument is not an object, an exception is thrown.
Modifiers
check.maybe.xxx(...): Returnstrueifthingisnullorundefined, otherwise it propagates the return value from its predicate.check.verify.xxx(...)/check.verify.maybe.xxx(...): Throws anErrorif the predicate returns false. The last argument is an optional message to be set on theErrorinstance.
Batch operations
check.map(things, functions): Maps each predicate from thefunctionsobject to the corresponding value fromthings, returning the hash of results. Similar tolikebut using functions instead of values. Supports nested objects.check.every(predicateResults): Returnstrueif all properties of thepredicateResultsobject aretrue,falseotherwise.check.any(predicateResults): Returnstrueis any property of thepredicateResultsobject istrue,falseotherwise.
Some examples
check.object(0);
// Returns falsecheck.maybe.object(null);
// Returns truecheck.not.object(0);
// Returns truecheck.verify.like({}, { foo: 'bar' }, 'Invalid object');
// Throws new Error('Invalid object')check.verify.maybe.like(undefined, { foo: 'bar' }, 'Invalid object');
// Doesn't throwcheck.verify.not.like({}, { foo: 'bar' }, 'Invalid object');
// Doesn't throwcheck.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 falsecheck.any(
check.map({
foo: 0,
bar: ''
}, {
foo: check.number,
bar: check.unemptyString
})
);
// Returns trueWhat 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 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.
