What is type-name?
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.
What are type-name's main functionalities?
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'
Other packages similar to type-name
typeof
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.
type-detect
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.
kind-of
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.
type-name
Just a reasonable type name
DESCRIPTION
typeName
function returns reasonable type name for input.
description | input | result |
---|
string literal | 'hoge' | 'string' |
number literal | 5 | 'number' |
boolean literal | false | 'boolean' |
regexp literal | /^not/ | 'RegExp' |
array literal | [] | 'Array' |
function expression | function () {} | 'function' |
String object | new String('hoge') | 'String' |
Number object | new Number('3') | 'Number' |
Boolean object | new Boolean('1') | 'Boolean' |
Date object | new Date() | 'Date' |
RegExp object | new RegExp('^not', 'g') | 'RegExp' |
Array object | new Array() | 'Array' |
Function object | new Function('x', 'y', 'return x + y') | 'function' |
Error object | new Error('error!') | 'Error' |
user-defined constructor | new Person('alice', 5) | 'Person' |
anonymous constructor | new AnonPerson('bob', 4) | '' |
NaN | NaN | 'number' |
Infinity | Infinity | 'number' |
Math | Math | 'Math' |
JSON | JSON | 'JSON' |
arguments object | (function(){ return arguments; })() | 'Arguments' |
null literal | null | 'null' |
undefined value | undefined | 'undefined' |
EXAMPLE
var typeName = require('type-name'),
assert = require('assert');
function Person(name, age) {
this.name = name;
this.age = age;
}
var AnonPerson = function(name, age) {
this.name = name;
this.age = age;
};
assert(typeName('hoge') === 'string');
assert(typeName(5) === 'number');
assert(typeName(false) === 'boolean');
assert(typeName(/^not/) === 'RegExp');
assert(typeName([]) === 'Array');
assert(typeName(function () {}) === 'function');
assert(typeName(new String('hoge')) === '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 Function('x', 'y', 'return x + y')) === 'function');
assert(typeName(new Error('error!')) === 'Error');
assert(typeName(new Person('alice', 5)) === 'Person');
assert(typeName(new AnonPerson('bob', 4)) === '');
assert(typeName(NaN) === 'number');
assert(typeName(Infinity) === 'number');
assert(typeName(Math) === 'Math');
assert(typeName(JSON) === 'JSON');
assert(typeName((function(){ return arguments; })()) === 'Arguments');
assert(typeName(null) === 'null');
assert(typeName(undefined) === 'undefined');
HOW TO USE
$ npm install --save type-name
or
$ bower install --save type-name
AUTHOR
LICENSE
Licensed under the MIT license.