truetype
Simple JS module to check types more concisely
Check type of a variable in JavaScript is not so easy.
The builtin operators typeof
, instanceof
and other methods are not precise to report the exact type of a variable.
So, this module aims to check types of variables with more useful returns.
Installation
You can install truetype with NPM, Yarn, Snipacks, Unpkg CDN...
NPM
npm i -S truetype
Yarn
yarn add truetype
Snipacks
snipacks add unpkg truetype.js truetype
CDN
Usage
ES6
CommonJS
var truetype =
AMD (RequireJS)
Instance
Just call it with your variable as argument:
const x = 'foo bar' type =
truetype
is a function that returns a custom Class Object with the following props and methods:
truetype(x).value
The value of x
itself.
truetype(x).instance()
Returns a string with the x
constructor name like Object
, Array
, String
, Number
...
The returned value can be a builtin or custom constructor name.
// returns Number { thisx = x } const bar = 1 // returns Foo
truetype(x).is{Instance}()
Predefined methods that check if x
type is {Instance}
and returns a Boolean.
// returns true // returns true // returns true // returns true // returns true // returns true // returns true // returns true
There are 5 special methods to check values:
// returns true // returns true // returns true // returns false // returns false
truetype(x).is(type)
Check if x
type is equal type
argument and returns a Boolean.
It's possible to check predefined and custom constructor types.
// returns false // returns true // returns true // returns false //Custom types { thisx = x } const bar = 1 // returns true