|
|
10 сар өмнө | |
|---|---|---|
| .. | ||
| LICENSE | 10 сар өмнө | |
| README.md | 10 сар өмнө | |
| index.d.ts | 10 сар өмнө | |
| index.js | 10 сар өмнө | |
| index.ts | 10 сар өмнө | |
| package.json | 10 сар өмнө | |
| type-detect.js | 10 сар өмнө | |
<img alt="type-detect" src="https://raw.githubusercontent.com/chaijs/type-detect/master/type-detect-logo.svg"/>
Improved typeof detection for node, Deno, and the browser.
<img
alt="license:mit"
src="https://img.shields.io/badge/license-mit-green.svg?style=flat-square"
/>
<img
alt="npm:?"
src="https://img.shields.io/npm/v/type-detect.svg?style=flat-square"
/>
<img
alt="build:?"
src="https://github.com/chaijs/type-detect/workflows/Build/badge.svg"
/>
<img
alt="coverage:?"
src="https://img.shields.io/coveralls/chaijs/type-detect/master.svg?style=flat-square"
/>
<img
alt="dependencies:?"
src="https://img.shields.io/npm/dm/type-detect.svg?style=flat-square"
/>
<img
alt="devDependencies:?"
src="https://img.shields.io/david/chaijs/type-detect.svg?style=flat-square"
/>
<img
alt="Join the Slack chat"
src="https://img.shields.io/badge/slack-join%20chat-E2206F.svg?style=flat-square"
/>
<img
alt="Join the Gitter chat"
src="https://img.shields.io/badge/gitter-join%20chat-D0104D.svg?style=flat-square"
/>
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:
import type from '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:
<script src="./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.
var type = require('type-detect');
Or, in the browser use case, after the