oftype
A JavaScript module for checking variable runtime type
Usage
var oftype = ;var x = 123;var y = true;var z = /abc/;var u = undefined;var n = null; { thisname = 'ABC';} var o = ; //check if variable x is of Number type.; //true //check if variable y is of Boolean type.; //true //check if variable z is of RegExp type.; //true //check if variable u is of undefined type.//undefined is a type which has an only value undefined.; //true //check if variable n is of null type.//stricly, null is no actually a type, but here we treat// it as a type just like how we treat undefined.; //true; //check if variable o is of Obj type.; //true; //check if variable o is of Object type.; //true;
API
The signature of oftype
is
;
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
: treatnull
value asObject
type, so with this option testingnull
againstObject
type returnstrue
. Default value of this option isfalse
. -
primitiveObject
: treat primitive object as primitive type. for example, testingnew Number(1)
against Number will returnfalse
when this option is set tofalse
; but will returntrue
when this option is set totrue
.
Alternatively, to avoid setting the options in every function call, the options could also be set globally:
oftypenullAsObject = true;oftypeprimitiveObject = true;
Test
Make sure mocha
is installed globally
npm test
License
MIT