What is check-types?
The check-types npm package provides a collection of functions for asserting types and values in JavaScript. It is designed to help developers enforce type safety and data integrity in their applications by offering a simple and intuitive API for type checking and validation.
What are check-types's main functionalities?
Primitive Type Checking
This feature allows you to check if a value is of a specific primitive type, such as a number, string, or boolean. The code sample demonstrates how to use the package to verify that a value is a number, a string, and a boolean.
const check = require('check-types');
console.log(check.number(42)); // true
console.log(check.string('hello')); // true
console.log(check.boolean(false)); // true
Object and Array Checking
With this feature, you can check if a value is an array or an object. The code sample shows how to validate that a value is an array and an object.
const check = require('check-types');
console.log(check.array([1, 2, 3])); // true
console.log(check.object({ key: 'value' })); // true
Function and Null Checking
This feature enables you to verify if a value is a function or null. The provided code sample illustrates how to check for a function and a null value.
const check = require('check-types');
console.log(check.function(function() {})); // true
console.log(check.null(null)); // true
Other packages similar to check-types
validator
Validator is a library for string validation and sanitization. It differs from check-types by focusing on strings and providing a wide range of string validation options, unlike check-types which offers broader type checking capabilities.
joi
Joi is an object schema description language and validator for JavaScript objects. It offers more comprehensive validation capabilities compared to check-types, including the ability to define complex validation rules for objects.
prop-types
Prop-types is a library for type checking of props in React applications. It is specifically designed for React and allows developers to document the intended types of properties passed to components. Unlike check-types, prop-types is tailored for React component prop validation.
check-types.js
A tiny JavaScript library
for checking types
and throwing exceptions.
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.
How tiny is it?
17.7 kb unminified with comments, 2.9 kb minified, 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.git
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.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 two types.
-
check.isXxxx(thing)
:
These functions are predicates,
returning true or false
depending on the type and value of thing
.
-
check.verifyXxxx(thing, message)
:
These functions call
their equivalent isXxxx
predicate
and throw an Error
if the result is false
.
Additionally, every exported function
is available with a maybe
modifier
that simply returns true
if the value is null
or undefined
,
otherwise it delegates to the original, unmodified function.
Calls to these functions look like
check.maybe.isXxxx(thing)
and
check.maybe.verifyXxxx(thing)
.
Finally, there are also 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.isString(thing)
:
Returns true
if thing
is a string,
false
otherwise.
-
check.verifyString(thing, message)
:
Throws an exception
unless thing
is a string.
-
check.isUnemptyString(thing)
:
Returns true
if thing
is a non-empty string,
false
otherwise.
-
check.verifyUnemptyString(thing, message)
:
Throws an exception
unless thing
is a non-empty string.
-
check.isLength(thing, length)
:
Returns true
if thing
has a length property
that matches the specified length,
false
otherwise.
-
check.verifyLength(thing, length, message)
:
Throws an exception
unless thing
has a length property
that matches the specified length.
Number functions
-
check.isNumber(thing)
:
Returns true
if thing
is a number,
false
otherwise.
In this case,
NaN
is not considered to be a number.
-
check.verifyNumber(thing, message)
:
Throws an exception
unless thing
is a number.
In this case,
NaN
is not considered to be a number.
-
check.isPositiveNumber(thing)
:
Returns true
if thing
is a number
greater than zero,
false
otherwise.
-
check.verifyPositiveNumber(thing, message)
:
Throws an exception
unless thing
is a number
greater than zero.
-
check.isNegativeNumber(thing)
:
Returns true
if thing
is a number
less than zero,
false
otherwise.
-
check.verifyNegativeNumber(thing, message)
:
Throws an exception
unless thing
is a number
less than zero.
-
check.isEvenNumber(thing)
:
Returns true
if thing
is an even number,
false
otherwise.
-
check.verifyEvenNumber(thing, message)
:
Throws an exception
unless thing
is an even number.
-
check.isOddNumber(thing)
:
Returns true
if thing
is an odd number,
false
otherwise.
-
check.verifyOddNumber(thing, message)
:
Throws an exception
unless thing
is an odd number.
Function functions
-
check.isFunction(thing)
:
Returns true
if thing
is function,
false
otherwise.
-
check.verifyFunction(thing, message)
:
Throws an exception
unless thing
is function.
Array functions
-
check.isArray(thing)
:
Returns true
if thing
is an array,
false
otherwise.
-
check.verifyArray(thing, message)
:
Throws an exception
unless thing
is an array.
-
check.isLength(thing, length)
:
Returns true
if thing
has a length property
that matches the specified length,
false
otherwise.
-
check.verifyLength(thing, length, message)
:
Throws an exception
unless thing
has a length property
that matches the specified length.
Date functions
-
check.isDate(thing)
:
Returns true
if thing
is a date,
false
otherwise.
-
check.verifyDate(thing, message)
:
Throws an exception
unless thing
is a date.
Object functions
-
check.isObject(thing)
:
Returns true
if thing
is a non-null, non-array, non-date object,
false
otherwise.
-
check.verifyObject(thing, message)
:
Throws an exception
unless thing
is a non-null, non-array, non-date object.
-
check.isEmptyObject(thing)
:
Returns true
if thing
is an empty object,
false
otherwise.
-
check.verifyEmptyObject(thing, message)
:
Throws an exception
unless thing
is an empty object.
-
check.isInstance(thing, prototype)
:
Returns true
if thing
is an instance of prototype
,
false
otherwise.
-
check.verifyInstance(thing, prototype, message)
:
Throws an exception
unless thing
is an instance of prototype
.
-
check.quacksLike(thing, duck)
:
Tests whether an object 'quacks like a duck'.
Returns true
if thing
has all of the properties of duck
,
false
otherwise.
If either argument is not an object,
an exception is thrown.
-
check.verifyQuack(thing, duck, message)
:
Throws an exception
unless thing
has all of the properties of duck
.
Modifiers
check.maybe.isXxxx(...)
/ check.maybe.verifyXxxx(...)
:
Returns true
if thing
is null
or undefined
,
otherwise it delegates to
the original, unmodified function.
Batch operations
-
check.map(things, functions)
:
Maps each predicate from the functions
object
to the corresponding value from things
,
returning the hash of results.
Similar to quacksLike
but using functions instead of values.
Supports nested objects.
-
check.every(predicateResults)
:
Returns true
if all properties of the predicateResults
object are true
,
false
otherwise.
-
check.any(predicateResults)
:
Returns true
is any property of the predicateResults
object is true
,
false
otherwise.
Some examples
check.isObject(null);
check.maybe.isObject(null);
check.verifyQuack('', [], 'Invalid array-like object');
check.maybe.verifyQuack(undefined, [], 'Invalid array-like object');
check.map({
foo: 2,
bar: {
baz: 'qux'
}
}, {
foo: check.isOddNumber,
bar: {
baz: check.isUnemptyString
}
});
check.every(
check.map({
foo: 0,
bar: ''
}, {
foo: check.isNumber,
bar: check.isUnemptyString
})
);
check.any(
check.map({
foo: 0,
bar: ''
}, {
foo: check.isNumber,
bar: check.isUnemptyString
})
);
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
.
What license is it released under?
MIT