4.1.0 • Published 11 months ago

type-detect v4.1.0

Weekly downloads
11,651,561
License
MIT
Repository
github
Last release
11 months ago

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 or @@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. To install it, type:

$ npm install type-detect

Deno

type-detect can be imported with the following line:

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:

<script src="./node_modules/type-detect/type-detect.js"></script>

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.

var type = require('type-detect');

Or, in the browser use case, after the tag,

var type = typeDetect;

array

assert(type([]) === 'Array');
assert(type(new Array()) === 'Array');

regexp

assert(type(/a-z/gi) === 'RegExp');
assert(type(new RegExp('a-z')) === 'RegExp');

function

assert(type(function () {}) === 'function');

arguments

(function () {
  assert(type(arguments) === 'Arguments');
})();

date

assert(type(new Date) === 'Date');

number

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(new Number(1)) === 'Number'); // note - the object version has a capital N

string

assert(type('hello world') === 'string');
assert(type(new String('hello')) === 'String'); // note - the object version has a capital S

null

assert(type(null) === 'null');
assert(type(undefined) !== 'null');

undefined

assert(type(undefined) === 'undefined');
assert(type(null) !== 'undefined');

object

var Noop = function () {};
assert(type({}) === 'Object');
assert(type(Noop) !== 'Object');
assert(type(new Noop) === 'Object');
assert(type(new Object) === 'Object');

ECMA6 Types

All new ECMAScript 2015 objects are also supported, such as Promises and Symbols:

assert(type(new Map() === 'Map');
assert(type(new WeakMap()) === 'WeakMap');
assert(type(new Set()) === 'Set');
assert(type(new WeakSet()) === 'WeakSet');
assert(type(Symbol()) === 'symbol');
assert(type(new Promise(callback) === 'Promise');
assert(type(new Int8Array()) === 'Int8Array');
assert(type(new Uint8Array()) === 'Uint8Array');
assert(type(new UInt8ClampedArray()) === 'Uint8ClampedArray');
assert(type(new Int16Array()) === 'Int16Array');
assert(type(new Uint16Array()) === 'Uint16Array');
assert(type(new Int32Array()) === 'Int32Array');
assert(type(new UInt32Array()) === 'Uint32Array');
assert(type(new Float32Array()) === 'Float32Array');
assert(type(new Float64Array()) === 'Float64Array');
assert(type(new ArrayBuffer()) === 'ArrayBuffer');
assert(type(new DataView(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:

var myObject = {};
myObject[Symbol.toStringTag] = 'myCustomType';
assert(type(myObject) === 'myCustomType');
@sinonjs/commonsdeep-eql@sinonjs/samsameasy-select-rnreact-native-bluetooth2specify-importsbabel-specify-imports@arisageha/react-lazyload@arisageha/react-lazyload-fixreact-native-template-rfbaseairscanairscan-examplereact-native-esc-pos-sahaab@borisovart/atol-kkt-moduledeneme323112@texttree/demo-bsa-reference-rcl@ntt_app/react-native-custom-notificationfdt-react-styleguidist@sonammalhotra/lotide@smishra17/lotide@cheapthrills/lotidereact-native-covid-sdkgql_din_mod@cujo-common/simple-hello-world-exampleopenid-passport@iobroker-community-adapters/iobroker.device-watcher@olivervorasai/sliderreact-native-printer-brothers@newhorizon-tech/dd-npm-package-templatereact-native-shekhar-bridge-test@nicholasjj/lotidewilscanner@oiti/documentoscopy-react-native@mink-opn/build-tokensquoc-testplginexpand-react-bridgeluminos-ui-coremeson-jssklif-ui-kitsklif-api@everything-registry/sub-chunk-2993jawwy-sdkjawwy_gamification_releasereact-native-sphereuisphereuijawwy_libraryreact-native-credit-card-pkgp149-tablesklif-uireact-native-jawwy_samplegriffin-ui-librarynebula-http-resiliencycomposizetap-unfunktape-chaitape-enzymetailwind-vector-effectsyncbackbasessvelvet-customtest-library-123test-haptik-libtest-npm-jjmessiwinx-form-winxwith-defer-eswontbreakweb-component-tester-bundlewoven-challenge-deploywifi_configuration_packageteapackage-tatespoorman297superset-plugin-chart-hello-world2supercluster-googlemaps-adapter-clonesstoreuxstanikionespotify-ds-sesparse-setspektrstp-cdktestapatest-zeo-collecttest-solidity-npmsvelte-component-libthe_helper_packagethe_helper_packagestext-based-game-enginevision-camera-plugin-face-detectorvision-camera-plugin-scan-facesvision-camera-base64-resizedvisigothvcloudcam-playkit-js-hlsvantiq-reactvue-axios-restvue-dev-clone@djgould/cypress-ai@dormammuuuuu/json-helper@donapot/mylibtest@dongwandonkim/lotide@dotconf-pro/dotconf-pro@dotconf-pro/dotenv@donkswap/community-token-list
4.1.0

11 months ago

4.0.8

7 years ago

4.0.7

7 years ago

4.0.6

7 years ago

4.0.5

8 years ago

4.0.4

8 years ago

4.0.3

8 years ago

4.0.2

8 years ago

4.0.1

8 years ago

4.0.0

9 years ago

3.0.0

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.0

10 years ago

0.1.2

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago