Improved typeof detection for node, Deno, and the browser.
Supported Browsers |
Chrome |
Edge |
Firefox |
Safari |
IE |
✅ |
✅ |
✅ |
✅ |
9, 10, 11 |
## What is Type-Detect?
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`](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-typeof-operator) or [`@@toStringTag`](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-symbol.tostringtag). It also normalizes some object names for consistency among browsers.
## Why?
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.
## Installation
### Node.js
`type-detect` is available on [npm](http://npmjs.org). To install it, type:
$ npm install type-detect
### Deno
`type-detect` can be imported with the following line:
```js
import type from 'https://deno.land/x/type_detect@v4.1.0/index.ts'
```
### Browsers
You can also use it within the browser; install via npm and use the `type-detect.js` file found within the download. For example:
```html
```
## Usage
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`.
```js
var type = require('type-detect');
```
Or, in the browser use case, after the