Skip to main content

ChaiJS type-detect

Improved typeof detection for [node](http://nodejs.org) and the browser.

license:mit tag:? build:? coverage:? npm:? dependencies:? devDependencies:?
Selenium Test Status
Join the Slack chat Join the Gitter chat

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.

Installation

Node.js

type-detect is available on npm. To install it, type:

$ npm install type-detect

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');

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');

string

assert(type('hello world') === 'string');
assert(type(new String('hello')) === 'string');

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');