Gathering detailed insights and metrics for type-detect
Gathering detailed insights and metrics for type-detect
Gathering detailed insights and metrics for type-detect
Gathering detailed insights and metrics for type-detect
Improved typeof detection for node.js and the browser.
npm install type-detect
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
130 Stars
183 Commits
30 Forks
15 Watching
4 Branches
21 Contributors
Updated on 20 Nov 2024
Minified
Minified + Gzipped
JavaScript (64.86%)
TypeScript (35.14%)
Cumulative downloads
Total Downloads
Last day
-4.1%
6,360,045
Compared to previous day
Last week
1.6%
34,159,240
Compared to previous week
Last month
15.3%
140,866,422
Compared to previous month
Last year
17.4%
1,400,467,267
Compared to previous year
34
Improved typeof detection for node, Deno, and the browser.
Supported Browsers | |||||
---|---|---|---|---|---|
Chrome | Edge | Firefox | Safari | IE | |
✅ | ✅ | ✅ | ✅ | 9, 10, 11 |
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:
1import 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:
1<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
.
1var type = require('type-detect');
Or, in the browser use case, after the
1var type = typeDetect;
1assert(type([]) === 'Array'); 2assert(type(new Array()) === 'Array');
1assert(type(/a-z/gi) === 'RegExp'); 2assert(type(new RegExp('a-z')) === 'RegExp');
1assert(type(function () {}) === 'function');
1(function () { 2 assert(type(arguments) === 'Arguments'); 3})();
1assert(type(new Date) === 'Date');
1assert(type(1) === 'number'); 2assert(type(1.234) === 'number'); 3assert(type(-1) === 'number'); 4assert(type(-1.234) === 'number'); 5assert(type(Infinity) === 'number'); 6assert(type(NaN) === 'number'); 7assert(type(new Number(1)) === 'Number'); // note - the object version has a capital N
1assert(type('hello world') === 'string'); 2assert(type(new String('hello')) === 'String'); // note - the object version has a capital S
1assert(type(null) === 'null'); 2assert(type(undefined) !== 'null');
1assert(type(undefined) === 'undefined'); 2assert(type(null) !== 'undefined');
1var Noop = function () {}; 2assert(type({}) === 'Object'); 3assert(type(Noop) !== 'Object'); 4assert(type(new Noop) === 'Object'); 5assert(type(new Object) === 'Object');
All new ECMAScript 2015 objects are also supported, such as Promises and Symbols:
1assert(type(new Map() === 'Map'); 2assert(type(new WeakMap()) === 'WeakMap'); 3assert(type(new Set()) === 'Set'); 4assert(type(new WeakSet()) === 'WeakSet'); 5assert(type(Symbol()) === 'symbol'); 6assert(type(new Promise(callback) === 'Promise'); 7assert(type(new Int8Array()) === 'Int8Array'); 8assert(type(new Uint8Array()) === 'Uint8Array'); 9assert(type(new UInt8ClampedArray()) === 'Uint8ClampedArray'); 10assert(type(new Int16Array()) === 'Int16Array'); 11assert(type(new Uint16Array()) === 'Uint16Array'); 12assert(type(new Int32Array()) === 'Int32Array'); 13assert(type(new UInt32Array()) === 'Uint32Array'); 14assert(type(new Float32Array()) === 'Float32Array'); 15assert(type(new Float64Array()) === 'Float64Array'); 16assert(type(new ArrayBuffer()) === 'ArrayBuffer'); 17assert(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:
1var myObject = {}; 2myObject[Symbol.toStringTag] = 'myCustomType'; 3assert(type(myObject) === 'myCustomType');
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 13/14 approved changesets -- score normalized to 9
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
48 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More