Description
of-type
is a very light module that checks if the given value is of the expected type (or types).
Also see typeof-arguments
to validate types of the arguments passed through functions.
Also see typeof-properties
to validate types of the objects' properties.
Implementation
with NodeJS
npm install of-type
const type = ; ; //true; //true; //true
with Browser
of-type.js
library to the HTML file.
Add The library is located in ./dist/of-type.js
directory.
It is a webpack&babel bundled cross-browser library version.
The library is accessible as ofType
variable in the global (window) scope.
Tests
> git clone https://github.com/devrafalko/of-type.git
> cd of-type
> npm install
> npm test //run tests in node
> npm test deep //run tests in node with errors shown
Usage
type(val, type)
val
It is any value|object which type should be checked.
type
[String|RegExp|null|undefined|Function|Array]
The val
value|object is expected to be of type
type. There are many ways to check the val
type. Choose the most convenient one:
[String]
- Possible values:
'null'
,'undefined'
- any value that equals to
val
.constructor.name
, eg:
'string'
,'number'
,'regexp'
,'array'
,'object'
,'boolean'
,'buffer'
, etc.
- The [String]
type
is case insensitive:'String'
,'string'
,'StRiNg'
checks if theval
is of[String]
type'RegExp'
,'REGEXP'
,'regexp'
checks if theval
is of[RegExp]
type
- The [String]
type
can contain multiple types, separated with|
:'array|object'
checks if theval
is of[Array]
OR
[Object]
type'undefined|null'
checks if theval
is ofundefined
OR
null
type
- See the samples
[RegExp]
- Possible values:
/null/
,/undefined/
- any value matching the
val
.constructor.name
, eg:/String/
,/Number/
,/RegExp/
,/Array/
,/Object/
,/Boolean/
,/Buffer/
,/Promise/
, etc.
- Use all regular expression's features to match the type in a desired way:
/Str/
,/Err/
,/Reg/
,/B/
/.+Error$/
,/^RegExp$/
,/^[A-Z][a-z]+$/
- For the case insensitivity use
i
flag:/string/i
,/regexp/i
,/TYPEERROR/i
- For multiple values use regexp
(x|y)
expression:/String|Number/
,/TypeError|Error/
,/(obj|str)/i
- See the samples
[Function|Array|null|undefined]
- Possible values:
null
,undefined
- any
[Function]
constructor, eg:String
,TypeError
,Promise
,Array
, etc.
- For multiple values use array:
[String, Object, Array, null]
[null, undefined, Boolean]
- See the samples
When you use bundlers or minifiers, use
[String|RegExp]
type
wisely as bundlers may change the names of functions|constructors|classes in the output file and eg.type(myInstance, 'MyClass')
that returnstrue
before compilation, may returnfalse
after compilation, if the bundler minifies the'MyClass'
constructor name.
Extra types:
[String] 'arguments'
| [RegExp] /arguments/
- The
type
'arguments'
or/arguments/
returnstrue
for the function'sarguments
object - See the samples
[String] 'instance'
| [RegExp] /instance/
- The
type
'instance'
or/instance/
returnstrue
for the instance of the user's class|constructortype(MyInstance, 'instance'); //true
- It returns
false
for instances of built-in (native) constructors[]
,'hello world'
,{}
- It returns
false
for instances that are theglobal
|window
's properties - See the samples
[String] 'objectable'
| [RegExp] /objectable/
- The
type
'objectable'
or/objectable/
returnstrue
for the objects that are the instances ofObject
constructor{}
,[]
,new String('hello world')
,new Boolean(1)
- It returns
false
for the primitive values and simple values'hello world'
,true
,10
,null
,undefined
- See the samples
[String] 'truthy'
| [RegExp] /truthy/
- The
type
'truthy'
or/truthy/
returnstrue
for the values like:'abc'
,true
,1
,-1
,{}
,[]
,function(){}
- See the samples
[String] 'falsy'
| [RegExp] /falsy/
- The
type
'falsy'
or/falsy/
returnstrue
for the values like:''
,false
,0
,null
,undefined
,NaN
- See the samples
[String] 'any'
| [RegExp] /any/
| [Array] []
| [String] ""
- The
type
'any'
or/any/
or empty array[]
or empty string""
returnstrue
for the values of any type - See the samples
Return value
The function type()
returns true
if the val
argument is of expected type
.
The function type()
returns false
if the val
argument is not of expected type
.
Tips
Missing the
val
ortype
arguments will always returnfalse
(without throwing error).
; //false ; //true ; //false
Samples
[String]
type
; ; //true; //true; //true; //true; //true; //false; //true; //true; //true; //true ; //true; //true; //false; //true; //true; //false; //true ; //true; //true; //true; //true ; //true; //true ; //false; //true; //true ; //true; //true; //true; //false ; //true; //false; //true ; //true; constructor.name === "Object" {};; //true; //true; //false; constructor.name === 'Name' ; //true; //false; //true
[RegExp]
type
; ; //true; //true; //true; //true; //true; //false; //true; //true; //true; //true ; //false; //false; //true; //true; //true; //true; //false; //false; //true; //true; //true; //true; //false; //true ; //true; //true; //false; //false ; //true; constructor.name === "Object" ; //true; //true; //true; //true; //true ; //true; //true; //true; //true; //true {};; //true; //false; //true; //false; constructor.name === 'Name'
constructor
, null
and undefined
; ; //true; //true; //true; //true; //false; //true; //true; //false; //true; //true; //true; //true; //true; constructor.name === "Object" ; //true; //true; //true; //true ; //true; //true ; //false; //true; //true ; //true; //true; //true; //false; //true {};; //true; //false; constructor.name === 'Name' ; //true; //false; //true
arguments
type
; { return arguments;} ; //true; //true; //false; //true; //false; //true; //false ; //true; //true
instance
type
; ; //true; //true; //false; //true; //false; //true; //false ; //false; //false; //false; //false //false globalFramework = ;windowCars = ; ; //false; //false; //false; //false ; //true; //true
objectable
type
;; //false; //false; //false; //false; //false; //true; //false; //true; //true; //true; //true; (arguments instanceof Object) === true {}; //true; //true; //true; //true ; //true; //true; //false; //true; //false; //true; //false; as it's unrecognizable custom type; //true; as it still matches constructor.name === 'Object' ; //true; //true; //true; //true
truthy
type
; ; //true; //false; //true; //false; //true; //false; //false; //false; //true; //true; //true; //false; //true; //true; //true; //false ; //true; //true; //false; //true; //false; //true; //false; as it's unrecognizable custom type ; //false; //true; //true
falsy
type
;; //false; //true; //false; //true; //false; //true; //true; //true; //false; //false; //false; //true; //false; //false; //false; //true ; //true; //true; //false; //true; //false; //true; //false; as it's unrecognizable custom type ; //true; //true; //true
any
type
; ; //true; //true; //true; //true; //true; //true; //true; //true; //true; //true; //true; //true; //true; //true; //true ; //true; //true; //false; //true; //false; //true; //false; as it's unrecognizable custom type