Returns type of value or object instance. An alternative to typeof operator.
npm install instype``text`
instype(typeof
Returns type of value or object instance.
This is an alternative to that actually allows to avoid confusion between the type name and the class name.
Returns the name of Class (or constructor function, or reserved type name), for example:
'Object', 'Number', 'String', 'NotNumber', 'Global', 'Dictionary', 'Null', 'Array', etc.
`javascript
const instype = require('instype')
instype(undefined) //=> 'Undefined'
instype(null) //=> 'Null' but not 'object'
instype(true) //=> 'Boolean'
instype(false) //=> 'Boolean'
instype(new Boolean(true)) //=> 'Boolean' but not 'object'
instype(new MyClass()) //=> 'MyClass' but not 'object'
instype(42) //=> 'Number'
instype(new Number(42)) //=> 'Number' but not 'object'
instype(1/0) //=> 'InfiniteNumber' but not 'number'
instype(-Infinity) //=> 'InfiniteNumber' but not 'number'
instype(NaN) //=> 'NotNumber' but not 'number'
instype('str') //=> 'String'
instype(new String('str')) //=> 'String' but not 'object'
instype({}) //=> 'Object'
instype(Object.create(null)) //=> 'Dictionary' but not 'object'
instype(new Object()) //=> 'Object'
instype(new Date()) //=> 'Date'
instype([1, 2, 3]) //=> 'Array'
instype(/a-z/) //=> 'RegExp' but not 'object'
instype(new RegExp('foo')) //=> 'RegExp' but not 'object'
instype(new Error('error')) //=> 'Error'
instype(new ReferenceError('')) //=> 'ReferenceError'
instype(function () {}) //=> 'Function'
instype(async function () {}) //=> 'AsyncFunction'
instype(() => {}) //=> 'Function'
instype(function * () {}) //=> 'GeneratorFunction'
instype(Symbol('str')) //=> 'Symbol'
instype(new Map()) //=> 'Map'
instype(new Int8Array()) //=> 'Int8Array'
instype(window) //=> 'Global' but not 'object'
`
Reserved type names make possible to distinguish type of some special values from values with the real type.
Historically, accessing the global object has required different syntax in different JavaScript environments. On the web you can use window, self, frames, in Node.js you must instead use global and type of this global value is 'object'. Also there are not straightforward situations with null, NaN and Infinity values.instype
To determine the type of such values the function returns the following type names:
- 'Undefined' for undefined (typeof(undefined) -> 'undefined')null
- 'Null' for (typeof(null) -> 'object')global
- 'Global' for , window, etc. (typeof(global) -> 'object')NaN
- 'NotNumber' for (typeof(NaN) -> 'number')Infinity
- 'InfiniteNumber' for or -Infinity (typeof(Infinity) -> 'number')object without prototype
- 'Dictionary' for (typeof(Object.create(null)) -> 'object') that was created by Object.create(null)
> Install on Node.JS with npm
`bash``
npm install instype
MIT © Taras Panasyuk