precise-typeof
Better typeof
. Detects real type of the objects like Array()
, new Number(1)
, new Boolean(true)
, etc.
compression | size |
---|
precise-typeof.js | 1.69 kB |
precise-typeof.min.js | 809 B |
precise-typeof.min.js.gz | 391 B |
Install
$ yarn add precise-typeof
or
$ npm install --save precise-typeof
Examples
var typeOf = require('precise-typeof');
typeOf({});
typeOf(new function(){});
typeOf([]);
typeOf(25);
typeOf(Infinity);
typeOf('ABC');
typeOf(function(){});
typeOf(Math.sin);
typeOf(undefined);
typeOf(true);
typeOf(null);
typeOf(NaN);
typeOf(new Object());
typeOf(new Array());
typeOf(new Number(5));
typeOf(new Number(Infinity));
typeOf(new String('ABC'));
typeOf(new Function('a', 'b', 'return a + b'));
typeOf(new Boolean());
typeOf(new Number('blabla'));
typeOf(new function Moment(){});
typeOf(/s/);
typeOf(new Date());
typeOf(Math);
typeOf(new Error());
typeOf(arguments);
typeOf(global);
typeOf(process);
typeOf(Buffer('B'));
typeOf(new Buffer(2));
typeOf(Buffer([62, 64, 66]));
typeOf(Symbol('A'));
typeOf(window);
typeOf(document);
typeOf(document.body);
typeOf(document.getElementsByTagName('html')[0]);
typeOf(document.getElementsByTagName('div'));
typeOf(document.createElement('a'));
typeOf(document.createTextNode('Abcd'));
typeOf(document.createComment('abcd'));
typeOf(document.createEvent('Event'));
typeOf(document.createEvent('UIEvents'));
typeOf(document.createEvent('HTMLEvents'));
typeOf(document.createEvent('MouseEvents'));
{ pojoOnly: true }
With pojoOnly
flag it only reports Plain-Old Javascript Objects as object
,
and reports "instances" by their constructor names (e.g. Moment
for moment
instance).
In case if object was created from the nameless function, it will be reported as unknown
.
var typeOf = require('precise-typeof');
typeOf(new function Moment(){}, {pojoOnly: true});
typeOf(new function ABC(){}, {pojoOnly: true});
typeOf(new function(){}, {pojoOnly: true});
typeOf({}, {pojoOnly: true});
typeOf([], {pojoOnly: true});
typeOf(25, {pojoOnly: true});
typeOf(Infinity, {pojoOnly: true});
typeOf('ABC', {pojoOnly: true});
typeOf(function(){}, {pojoOnly: true});
typeOf(Math.sin, {pojoOnly: true});
typeOf(undefined, {pojoOnly: true});
typeOf(true, {pojoOnly: true});
typeOf(null, {pojoOnly: true});
typeOf(NaN, {pojoOnly: true});
typeOf(new Object(), {pojoOnly: true});
typeOf(new Array(), {pojoOnly: true});
typeOf(new Number(5), {pojoOnly: true});
typeOf(new Number(Infinity), {pojoOnly: true});
typeOf(new String('ABC'), {pojoOnly: true});
typeOf(new Function(), {pojoOnly: true});
typeOf(new Boolean(), {pojoOnly: true});
typeOf(new Number('blabla'), {pojoOnly: true});
License
Precise-TypeOf is released under the MIT license.