
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
type-detect
Advanced tools
type-detect
Improved typeof detection for node and the browser.
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.
type-detect is available on npm. To install it, type:
$ npm install type-detect
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');
assert(type([]) === 'array');
assert(type(new Array()) === 'array');
assert(type(/a-z/gi) === 'regexp');
assert(type(new RegExp('a-z')) === 'regexp');
assert(type(function () {}) === 'function');
(function () {
assert(type(arguments) === 'arguments');
})();
assert(type(new Date) === 'date');
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');
assert(type('hello world') === 'string');
assert(type(new String('hello')) === 'string');
assert(type(null) === 'null');
assert(type(undefined) !== 'null');
assert(type(undefined) === 'undefined');
assert(type(null) !== 'undefined');
var Noop = function () {};
assert(type({}) === 'object');
assert(type(Noop) !== 'object');
assert(type(new Noop) === 'object');
assert(type(new Object) === 'object');
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');
The 'is' package provides a comprehensive set of type-check functions, similar to type-detect. It includes checks for primitive types, built-in objects, and also offers functions to test for specific conditions (like whether a number is integer). Compared to type-detect, 'is' might offer a more function-oriented approach for type checking.
The 'check-types' package offers a similar functionality to type-detect, focusing on providing a large set of assertion functions to validate the types of variables. It supports a wide range of types and conditions, including checks for truthy or falsy values. 'check-types' might be preferred in scenarios where assertions are more suitable than simple type detection.
FAQs
Improved typeof detection for node.js and the browser.
The npm package type-detect receives a total of 42,326,144 weekly downloads. As such, type-detect popularity was classified as popular.
We found that type-detect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.