Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
The type-name npm package is used to get the name of a JavaScript value's type. It is particularly useful for debugging and logging purposes, where knowing the exact type of a value can be crucial.
Basic Type Identification
This feature allows you to identify the basic types of JavaScript values such as numbers, strings, and booleans.
const typeName = require('type-name');
console.log(typeName(123)); // 'Number'
console.log(typeName('hello')); // 'String'
console.log(typeName(true)); // 'Boolean'
Object Type Identification
This feature helps in identifying the types of more complex objects like arrays, dates, and plain objects.
const typeName = require('type-name');
console.log(typeName({})); // 'Object'
console.log(typeName([])); // 'Array'
console.log(typeName(new Date())); // 'Date'
Custom Class Type Identification
This feature allows you to identify instances of custom classes, which can be particularly useful in larger codebases with many custom types.
const typeName = require('type-name');
class MyClass {}
console.log(typeName(new MyClass())); // 'MyClass'
The 'typeof' operator is a built-in JavaScript operator that returns a string indicating the type of the unevaluated operand. While it is more limited compared to type-name, it is a native feature and does not require any additional package.
The 'type-detect' package is another npm package that provides type detection for JavaScript values. It offers more detailed type information compared to the built-in 'typeof' operator and is similar in functionality to type-name.
The 'kind-of' package is a utility for getting the native type of a value. It is similar to type-name but offers additional features like checking if a value is a buffer, stream, or other specific types.
Just a reasonable typeof
typeName
function returns reasonable type name for input value.
description | input | result |
---|---|---|
null literal | null | 'null' |
undefined value | undefined | 'undefined' |
string literal | 'foo' | 'string' |
number literal | 5 | 'number' |
boolean literal | false | 'boolean' |
regexp literal (Android 4.1+) | /^not/ | 'RegExp' |
array literal | ['foo', 4] | 'Array' |
object literal | {name: 'bar'} | 'Object' (be careful!) |
function expression | function () {} | 'function' |
String object | new String('foo') | 'String' |
Number object | new Number('3') | 'Number' |
Boolean object | new Boolean('1') | 'Boolean' |
Date object | new Date() | 'Date' |
RegExp object (Android 4.1+) | new RegExp('^not', 'g') | 'RegExp' |
Array object | new Array() | 'Array' |
Object object | new Object() | 'Object' |
Function object | new Function('x', 'y', 'return x + y') | 'function' (be careful!) |
Error object | new Error('error!') | 'Error' |
TypeError object | new TypeError('type error!') | 'TypeError' |
NaN | NaN | 'number' |
Infinity | Infinity | 'number' |
Math | Math | 'Math' |
JSON (IE8+) | JSON | 'JSON' |
arguments object (IE9+) | (function(){ return arguments; })() | 'Arguments' |
User-defined constructor | new Person('alice', 5) | 'Person' |
Anonymous constructor | new AnonPerson('bob', 4) | '' |
Named class | new(class Foo { constructor() {} }) | 'Foo' |
Anonymous class | new(class { constructor() {} }) | '' |
Symbol | Symbol("FOO") | 'symbol' |
Promise | Promise.resolve(1) | 'Promise' |
var typeName = require('type-name');
var assert = require('assert');
assert(typeName(null) === 'null');
assert(typeName(undefined) === 'undefined');
assert(typeName('foo') === 'string');
assert(typeName(5) === 'number');
assert(typeName(false) === 'boolean');
assert(typeName(/^not/) === 'RegExp');
assert(typeName(['foo', 4]) === 'Array');
assert(typeName({name: 'bar'}) === 'Object');
assert(typeName(function () {}) === 'function');
assert(typeName(new String('foo')) === 'String');
assert(typeName(new Number('3')) === 'Number');
assert(typeName(new Boolean('1')) === 'Boolean');
assert(typeName(new Date()) === 'Date');
assert(typeName(new RegExp('^not', 'g')) === 'RegExp');
assert(typeName(new Array()) === 'Array');
assert(typeName(new Object()) === 'Object');
assert(typeName(new Function('x', 'y', 'return x + y')) === 'function');
assert(typeName(new Error('error!')) === 'Error');
assert(typeName(new TypeError('type error!')) === 'TypeError');
assert(typeName(NaN) === 'number');
assert(typeName(Infinity) === 'number');
assert(typeName(Math) === 'Math');
assert(typeName(JSON) === 'JSON'); // IE8+
assert(typeName((function(){ return arguments; })()) === 'Arguments'); // IE9+
assert(typeName(Symbol("FOO")) === 'symbol');
assert(typeName(Promise.resolve(1)) === 'Promise');
function Person(name, age) {
this.name = name;
this.age = age;
}
var AnonPerson = function(name, age) {
this.name = name;
this.age = age;
};
assert(typeName(new Person('alice', 5)) === 'Person');
assert(typeName(new AnonPerson('bob', 4)) === '');
assert(typeName(new(class Foo { constructor() {} })) === 'Foo');
assert(typeName(new(class { constructor() {} })) === '');
Install
$ npm install --save type-name
Use
var typeName = require('type-name');
console.log(typeName(anyVar));
typeName
function is exported
<script type="text/javascript" src="./path/to/node_modules/type-name/build/type-name.js"></script>
Install
$ bower install --save type-name
Load (typeName
function is exported)
<script type="text/javascript" src="./path/to/bower_components/type-name/build/type-name.js"></script>
Use
console.log(typeName(anyVar));
Licensed under the MIT license.
FAQs
Just a reasonable typeof
We found that type-name 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.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.