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.
types.coffee
A small coffeescript library for checking types and throwing exceptions.
Installation
npm install check-types
Import the module using require
, referencing either
the raw coffeescript (src/types.coffee
, 1.8 kb), the
compiled javascript (build/types.js
, 2.8 kb) or
minified javascript (src/types.min.js
1.2 kb).
To call the library in a browser environment, use OneJS
or Browserify.
Usage
types.coffee exports a number of different functions:
quacksLike (thing, duck)
Tests whether an object 'quacks like a duck'. Returns true
if the first argument has all of the properties of the second,
archetypal argument (the 'duck'). Returns false
otherwise.
If either argument is not an object, an exception is thrown.
isInstance (thing, prototype)
Returns true
if an object is an instance of a prototype,
false
otherwise.
verifyInstance (thing, prototype, message)
Throws an exception if an object is not an instance of a
prototype.
isObject (thing)
Returns true
if something is a non-null, non-array object,
false
otherwise.
verifyObject (thing, message)
Throws an exception unless something is a non-null, non-array
object.
isArray (thing)
Returns true
something is an array, false
otherwise.
verifyArray (thing, message)
Throws an exception unless something is an array.
isFunction (thing)
Returns true
if something is function, false
otherwise.
verifyFunction (thing, message)
Throws an exception unless something is function.
isUnemptyString (thing)
Returns true
if something is a non-empty string, false
otherwise.
verifyUnemptyString (thing, message)
Throws an exception unless something is a non-empty string.
isString (thing)
Returns true
if something is a string, false
otherwise.
verifyString (thing, message)
Throws an exception unless something is a string.
Development
Dependencies
The build environment relies on Node.js, NPM, Jake,
CoffeeScript, CoffeeLint, 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
.
Unit tests
The unit tests are in test/types.coffee
. You can run them
with the command npm test
or jake jstest
.