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 little JavaScript library for asserting types and values.
- Why would I want that?
- How little is it?
- How do I install it?
- How do I use it?
- Where can I use it?
- What changed from 3.x to 4.x?
- What changed from 2.x to 3.x?
- What changed from 1.x to 2.x?
- 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 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?
22 kb unminified with comments, 5.8 kb minified, 2 kb minified + gzipped.
How do I install it?
If you're using npm:
npm install check-types --saveOr if you just want the git repo:
git clone git@github.com:philbooth/check-types.js.gitIf 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 have 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:
check.xxx(thing): These functions are basic predicates, returning true or false depending on the type and value ofthing.check.not.xxx(thing): Thenotmodifier negates predicates, returningtrueif the predicate returnsfalseandfalseif the predicate returnstrue. It is also itself a function, which simply returns the negation of its argument.check.maybe.xxx(thing): Themaybemodifier tweaks predicates to returntrueifthingisnullorundefined, otherwise their normal result is returned. It is also itself a function, which returnstruewhen its argument isnullorundefined, otherwise it returns its argument.check.either.xxx(thing).or.yyy(thang): Theeithermodifier returnstrueif either predicate is true, it will only returnfalsewhen both predicates are false.check.assert.xxx(thing, message): Theassertmodifier changes predicates to throw when their result would otherwise befalse. It can be applied to thenot,maybeandeithermodifiers using the formscheck.assert.not.xxx(thing, message),check.assert.maybe.xxx(thing, message)andcheck.assert.either.xxx(thing, message).or.yyy(thang). It is also itself a function, which simply throws when its argument is false.check.array.of.xxx(thing): Thearray.ofmodifier first checks that it is operating on an array and then applies the modified predicate to each item of the array. If the predicate fails for any item, it returnsfalse, otherwise it returnstrue. It can also be prefixed by other modifiers, socheck.maybe.array.of,check.not.array.of,check.assert.array.of,check.assert.maybe.array.ofandcheck.assert.not.array.ofall work as you would expect them to.check.arrayLike.of.xxx(thing): ThearrayLike.ofmodifier is synonymous witharray.of, except it operates on array-like objects.check.iterable.of.xxx(thing): Theiterable.ofmodifier is synonymous witharray.of, except it operates on iterables.check.object.of.xxx(thing): Theobject.ofmodifier is synonymous witharray.of, except it operates on an object's properties.
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.
String predicates
check.string(thing): Returnstrueifthingis a string,falseotherwise.check.nonEmptyString(thing): Returnstrueifthingis a non-empty string,falseotherwise.check.contains(thing, substring): Returnstrueifthingis a string that containssubstring,falseotherwise.check.match(thing, regex): Returnstrueifthingis a string that matchesregex,falseotherwise.check.hasLength(thing, value): Returnstrueifthinghas a length property that equalsvalue,falseotherwise.
Number predicates
check.number(thing): Returnstrueifthingis a number,falseotherwise. Note thatNaN,Number.POSITIVE_INFINITYandNumber.NEGATIVE_INFINITYare not considered numbers here.check.greater(thing, value): Returnstrueifthingis a number greater thanvalue,falseotherwise.check.greaterOrEqual(thing, value): Returnstrueifthingis a number greater than or equal tovalue,falseotherwise.check.less(thing, value): Returnstrueifthingis a number less thanvalue,falseotherwise.check.lessOrEqual(thing, value): Returnstrueifthingis a number less than or equal tovalue,falseotherwise.check.between(thing, a, b): Returnstrueifthingis a number between thanaandb,falseotherwise. The argumentsaandbmay be in any order, it doesn't matter which is greater.check.inRange(thing, a, b): Returnstrueifthingis a number in the rangea..b,falseotherwise. The argumentsaandbmay be in any order, it doesn't matter which is greater.check.positive(thing): Returnstrueifthingis a number greater than zero,falseotherwise.check.negative(thing): Returnstrueifthingis a number less than zero,falseotherwise.check.zero(thing): Returnstrueifthingis zero,falseotherwise.check.odd(thing): Returnstrueifthingis an odd number,falseotherwise.check.even(thing): Returnstrueifthingis an even number,falseotherwise.check.integer(thing): Returnstrueifthingis an integer,falseotherwise.
Boolean predicates
check.boolean(thing): Returnstrueifthingis a boolean,falseotherwise.
Array predicates
check.array(thing): Returnstrueifthingis an array,falseotherwise.check.emptyArray(thing): Returnstrueifthingis an empty array,falseotherwise.check.hasLength(thing, value): Returnstrueifthinghas a length property that equalsvalue,falseotherwise.check.arrayLike(thing): Returnstrueifthinghas a numeric length property,falseotherwise.check.iterable(thing): Returnstrueifthingimplements the iterable protocol,falseotherwise. In pre-ES6 environments, this predicate falls back toarrayLikebehaviour.
Object predicates
check.object(thing): Returnstrueifthingis a plain-old JavaScript 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.
Date predicates
check.date(thing): Returnstrueifthingis a valid date,falseotherwise.
Error predicates
check.error(thing): Returnstrueifthingis an error,falseotherwise.
Function predicates
check.function(thing): Returnstrueifthingis a function,falseotherwise.check.hasLength(thing, value): Returnstrueifthinghas a length property that equalsvalue,falseotherwise.
Other predicates
check.null(thing): Returnstrueifthingisnull,falseotherwise.check.undefined(thing): Returnstrueifthingisundefined,falseotherwise.check.assigned(thing): Returnstrueifthingis notnullorundefined,falseotherwise.
Modifiers
check.not(value): Returns the negation ofvalue.check.not.xxx(...): Returns the negation of the predicate.check.maybe(value): Returnstrueifvalueisnullorundefined, otherwise it returnsvalue.check.maybe.xxx(...): Returnstrueifthingisnullorundefined, otherwise it propagates the return value from its predicate.check.either.xxx(...).or.yyy(...): Returnstrueif either predicate is true. Returnsfalseif both predicates are false.check.array.of.xxx(value): Returnstrueifvalueis an array and the predicate is true for every item. Also works with thenotandmaybemodifiers.check.arrayLike.of.xxx(thing): ThearrayLike.ofmodifier is synonymous witharray.of, except it operates on array-like objects.check.iterable.of.xxx(thing): Theiterable.ofmodifier is synonymous witharray.of, except it operates on iterables.check.object.of.xxx(thing): Theobject.ofmodifier is synonymous witharray.of, except it operates on an object's properties.check.assert(value, message): Throws anErrorifvalueisfalse, settingmessageon theErrorinstance.check.assert.xxx(...): Throws anErrorif the predicate returns false. The last argument is an optional message to be set on theErrorinstance. Also works with thenot,maybe,eitherand...ofmodifiers.
Batch operations
check.apply(things, predicates): Applies each value from thethingsarray to the corresponding predicate and returns the array of results. Passing a single predicate instead of an array applies all of the values to the same predicate.check.map(things, predicates): Maps each value from thethingsobject to the corresponding predicate and returns an object containing the results. Supports nested objects. Passing a single predicate instead of an object applies all of the values to the same predicate, ignoring nested objects.check.all(results): Returnstrueif all the result values are true in an array (returned fromapply) or object (returned frommap).check.any(predicateResults): Returnstrueif any result value is true in an array (returned fromapply) or object (returned frommap).
Some examples
check.even(3);
// Returns falsecheck.not.even(3);
// Returns truecheck.maybe.even(null);
// Returns truecheck.either.even(3).or.odd(3);
// Returns truecheck.assert.like({ foo: 'bar' }, { baz: 'qux' }, 'Invalid object');
// Throws new Error('Invalid object')check.assert.not.like({ foo: 'bar' }, { baz: 'qux' }, 'Invalid object');
// Doesn't throwcheck.assert.maybe.like(undefined, { foo: 'bar' }, 'Invalid object');
// Doesn't throwcheck.assert(myFunction(), 'Something went wrong');
// Throws 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 truecheck.any(
check.apply(
[ 1, 2, 3, '' ],
check.string
)
);
// Returns trueWhere 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 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.
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:
- Support for ES3 was dropped
- The predicates
gitUrl,emailandfloatNumberwere removed. verifywas renamed toassert.nulledwas renamed tonull.oddNumberwas renamed toodd.evenNumberwas renamed toeven.positiveNumberwas renamed topositive.negativeNumberwas renamed tonegative.intNumberwas renamed tointeger.boolwas renamed toboolean.definedwas swapped to becomeundefined.webUrlwas tightened to reject more cases.
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 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.
