Improved typeof detection for node, Deno, and the browser.
Type Detect is a module which you can use to detect the type of a given object. It returns a string representation of the object's type, either using typeof or @@toStringTag. It also normalizes some object names for consistency among browsers.
The typeof operator will only specify primitive values; everything else is "object" (including null, arrays, regexps, etc). Many developers use Object.prototype.toString() - which is a fine alternative and returns many more types (null returns [object Null], Arrays as [object Array], regexps as [object RegExp] etc).
Sadly, Object.prototype.toString is slow, and buggy. By slow - we mean it is slower than typeof. By buggy - we mean that some values (like Promises, the global object, iterators, dataviews, a bunch of HTML elements) all report different things in different browsers.
type-detect fixes all of the shortcomings with Object.prototype.toString. We have extra code to speed up checks of JS and DOM objects, as much as 20-30x faster for some values. type-detect also fixes any consistencies with these objects.
type-detect is available on npm. To install it, type:
$ npm install type-detect
type-detect can be imported with the following line:
importtypefrom'https://deno.land/x/type_detect@v4.1.0/index.ts'You can also use it within the browser; install via npm and use the type-detect.js file found within the download. For example:
<scriptsrc="./node_modules/type-detect/type-detect.js"></script>The primary export of type-detect is function that can serve as a replacement for typeof. The results of this function will be more specific than that of native typeof.
vartype=require('type-detect');Or, in the browser use case, after the <script> tag,
vartype=typeDetect;assert(type([])==='Array');assert(type(newArray())==='Array');assert(type(/a-z/gi)==='RegExp');assert(type(newRegExp('a-z'))==='RegExp');assert(type(function(){})==='function');(function(){assert(type(arguments)==='Arguments');})();assert(type(newDate)==='Date');assert(type(1)==='number');assert(type(1.234)==='number');assert(type(-1)==='number');assert(type(-1.234)==='number');assert(type(Infinity)==='number');assert(type(NaN)==='number');assert(type(newNumber(1))==='Number');// note - the object version has a capital Nassert(type('hello world')==='string');assert(type(newString('hello'))==='String');// note - the object version has a capital Sassert(type(null)==='null');assert(type(undefined)!=='null');assert(type(undefined)==='undefined');assert(type(null)!=='undefined');varNoop=function(){};assert(type({})==='Object');assert(type(Noop)!=='Object');assert(type(newNoop)==='Object');assert(type(newObject)==='Object');All new ECMAScript 2015 objects are also supported, such as Promises and Symbols:
assert(type(newMap()==='Map');assert(type(newWeakMap())==='WeakMap');assert(type(newSet())==='Set');assert(type(newWeakSet())==='WeakSet');assert(type(Symbol())==='symbol');assert(type(newPromise(callback)==='Promise');assert(type(newInt8Array())==='Int8Array');assert(type(newUint8Array())==='Uint8Array');assert(type(newUInt8ClampedArray())==='Uint8ClampedArray');assert(type(newInt16Array())==='Int16Array');assert(type(newUint16Array())==='Uint16Array');assert(type(newInt32Array())==='Int32Array');assert(type(newUInt32Array())==='Uint32Array');assert(type(newFloat32Array())==='Float32Array');assert(type(newFloat64Array())==='Float64Array');assert(type(newArrayBuffer())==='ArrayBuffer');assert(type(newDataView(arrayBuffer))==='DataView');Also, if you use Symbol.toStringTag to change an Objects return value of the toString() Method, type() will return this value, e.g:
varmyObject={};myObject[Symbol.toStringTag]='myCustomType';assert(type(myObject)==='myCustomType');