• Stars
    star
    130
  • Rank 277,575 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Improved typeof detection for node.js and the browser.

type-detect


Improved typeof detection for node, Deno, and the browser.

license:mit npm:? build:? coverage:? dependencies:? devDependencies:?
Join the Slack chat Join the Gitter chat

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 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/[email protected]/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 <script> 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');

More Repositories

1

chai

BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
JavaScript
8,128
star
2

chai-as-promised

Extends Chai with assertions about promises.
JavaScript
1,421
star
3

sinon-chai

Extends Chai with assertions for the Sinon.JS mocking framework.
JavaScript
1,089
star
4

chai-http

HTTP Response assertions for the Chai Assertion Library.
JavaScript
634
star
5

chai-jquery

jQuery assertions for chai
JavaScript
372
star
6

chai-spies

Spies for Chai Assertion Library.
JavaScript
132
star
7

deep-eql

Improved deep equality testing for Node.js and the browser.
JavaScript
108
star
8

chai-things

Chai support for assertions on array elements
CoffeeScript
104
star
9

chai-json-schema

Chai plugin for JSON Schema v4
JavaScript
75
star
10

chaijs.github.io

The chaijs.com website source code. Contributions welcome.
JavaScript
49
star
11

pathval

Object value retrieval given a string path
JavaScript
42
star
12

chai-fs

Chai assertions for Node.js filesystem
JavaScript
33
star
13

assertion-error

Error constructor for test and validation frameworks that implements standardized AssertionError specification.
TypeScript
25
star
14

chai-factories

Factories over fixtures. Chai Assertion Library.
JavaScript
23
star
15

loupe

Inspect utility for Node.js and browsers
JavaScript
21
star
16

chai-stats

Statistical and additional numerical assertions for the Chai Assertion Library.
JavaScript
16
star
17

check-error

Error comparison and information related utility for node and the browser
JavaScript
14
star
18

chai-change

Assert that a change you expected to happen, happened, with this chai plugin
JavaScript
14
star
19

get-func-name

Reliably get the name of a Function in a cross-browser compatible way.
JavaScript
12
star
20

simple-assert

Vanilla Assertions
JavaScript
7
star
21

chai-null

Null Object Pattern implementation for the Chai Assertion Library
JavaScript
5
star
22

chai-timers

Timers, stopwatches, and other time based assertions for the Chai Assertion Library.
JavaScript
5
star
23

guidelines

Guidelines for the Chaijs Organisation
1
star
24

plugin-use

A simple Event Emitter, tuned to be used as a plugin driver.
TypeScript
1
star