Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
type-detect
Advanced tools
The type-detect npm package is a utility for accurately detecting the types of JavaScript values. It goes beyond the basic `typeof` operator by being able to distinguish between more specific types, such as differentiating between an array and an object, or identifying specific types of objects like Date, RegExp, etc. This can be particularly useful in situations where strict type checking is necessary, such as input validation, or when working with complex data structures.
Detecting basic JavaScript types
This feature allows for the detection of basic JavaScript types such as number, boolean, string, null, and undefined. It provides a more reliable method than the `typeof` operator for these types.
"use strict";\nconst typeDetect = require('type-detect');\nconsole.log(typeDetect(1)); // 'number'\nconsole.log(typeDetect(true)); // 'boolean'\nconsole.log(typeDetect('Hello')); // 'string'\nconsole.log(typeDetect(null)); // 'null'\nconsole.log(typeDetect(undefined)); // 'undefined'
Distinguishing between arrays, objects, and null
This feature showcases the ability of type-detect to accurately distinguish between arrays, plain objects, and null, which is a common source of confusion when using the `typeof` operator.
"use strict";\nconst typeDetect = require('type-detect');\nconsole.log(typeDetect([])); // 'Array'\nconsole.log(typeDetect({})); // 'Object'\nconsole.log(typeDetect(null)); // 'null'
Identifying complex built-in types
This demonstrates type-detect's capability to identify more complex built-in JavaScript types such as Date objects and RegExp objects, which are not distinguishable using `typeof`.
"use strict";\nconst typeDetect = require('type-detect');\nconsole.log(typeDetect(new Date())); // 'Date'\nconsole.log(typeDetect(/regex/)); // 'RegExp'
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.
Improved typeof detection for node.js and the browser.
type-detect
is available on npm.
$ npm install type-detect
type-detect
is available as a component.
$ component install chaijs/type-detect
The primary export of type-detect
is function that can server 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('array' === type([]));
assert('array' === type(new Array()));
assert('regexp' === type(/a-z/gi));
assert('regexp' === type(new RegExp('a-z')));
assert('function' === type(function () {}));
(function () {
assert('arguments' === type(arguments));
})();
assert('date' === type(new Date));
assert('number' === type(1));
assert('number' === type(1.234));
assert('number' === type(-1));
assert('number' === type(-1.234));
assert('number' === type(Infinity));
assert('number' === type(NaN));
assert('string' === type('hello world'));
assert('null' === type(null));
assert('null' !== type(undefined));
assert('undefined' === type(undefined));
assert('undefined' !== type(null));
var Noop = function () {};
assert('object' === type({}));
assert('object' !== type(Noop));
assert('object' === type(new Noop));
assert('object' === type(new Object));
assert('object' === type(new String('hello')));
A Library
is a small constructed repository for custom type detections.
var lib = new type.Library;
Expose replacement typeof
detection to the library.
if ('string' === lib.of('hello world')) {
// ...
}
Add a test to for the .test()
assertion.
Can be defined as a regular expression:
lib.define('int', /^[0-9]+$/);
... or as a function:
lib.define('bln', function (obj) {
if ('boolean' === lib.of(obj)) return true;
var blns = [ 'yes', 'no', 'true', 'false', 1, 0 ];
if ('string' === lib.of(obj)) obj = obj.toLowerCase();
return !! ~blns.indexOf(obj);
});
Assert that an object is of type. Will first check natives, and if that does not pass it will use the user defined custom tests.
assert(lib.test('1', 'int'));
assert(lib.test('yes', 'bln'));
(The MIT License)
Copyright (c) 2013 Jake Luer jake@alogicalparadox.com (http://alogicalparadox.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Improved typeof detection for node.js and the browser.
The npm package type-detect receives a total of 26,992,060 weekly downloads. As such, type-detect popularity was classified as popular.
We found that type-detect demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.