oftype
A JavaScript module for checking variable runtime type
Usage
var oftype = require('oftype');
var x = 123;
var y = true;
var z = /abc/;
var u = undefined;
var n = null;
function Obj() {
this.name = 'ABC';
}
var o = new Obj();
oftype(123, Number);
oftype(y, Boolean);
oftype(z, RegExp);
oftype(u, undefined);
oftype(n, null);
oftype(o, Obj);
oftype(o, Object);
API
The signature of oftype
is
oftype(value, type, options);
value
: could be any variable or value;
-
type
: could be any type identifier, e.g.: Number, Boolean, String, Date, RegExp, Array, Object and any other customized complex type.
-
options
is optional and has below hash values:
-
nullAsObject
: treat null
value as Object
type, so with this option testing null
against Object
type returns true
. Default value of this option is false
.
-
primitiveObject
: treat primitive object as primitive type. for example, testing new Number(1)
against Number will return false
when this option is set to false
; but will return true
when this option is set to true
.
Alternatively, to avoid setting the options in every function call, the options could also be set globally:
oftype.nullAsObject = true;
oftype.primitiveObject = true;
Test
Make sure mocha
is installed globally
npm test
License
MIT