333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
type-name
================================
 
Just a reasonable `typeof`
 
[![Build Status][travis-image]][travis-url]
[![NPM version][npm-image]][npm-url]
[![Dependency Status][depstat-image]][depstat-url]
[![License][license-image]][license-url]
 
[![Sauce Test Status][saucelabs-image]][saucelabs-url]
 
 
DESCRIPTION
---------------------------------------
 
`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'` |
 
 
EXAMPLE
---------------------------------------
 
```javascript
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
---------------------------------------
 
### via npm
 
Install
 
    $ npm install --save type-name
 
Use
 
```javascript
var typeName = require('type-name');
console.log(typeName(anyVar));
```
 
#### use type-name npm module on browser
 
`typeName` function is exported
 
    <script type="text/javascript" src="./path/to/node_modules/type-name/build/type-name.js"></script>
 
 
### via bower
 
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
 
```javascript
console.log(typeName(anyVar));
```
 
 
AUTHOR
---------------------------------------
* [Takuto Wada](https://github.com/twada)
 
 
CONTRIBUTORS
---------------------------------------
* [azu](https://github.com/azu)
* [Yosuke Furukawa](https://github.com/yosuke-furukawa)
* [Athan](https://github.com/kgryte)
* [Andrew Moss](https://github.com/inversion)
 
 
LICENSE
---------------------------------------
Licensed under the [MIT](https://github.com/twada/type-name/blob/master/LICENSE) license.
 
 
[npm-url]: https://npmjs.org/package/type-name
[npm-image]: https://badge.fury.io/js/type-name.svg
 
[travis-url]: https://travis-ci.org/twada/type-name
[travis-image]: https://secure.travis-ci.org/twada/type-name.svg?branch=master
 
[depstat-url]: https://gemnasium.com/twada/type-name
[depstat-image]: https://gemnasium.com/twada/type-name.svg
 
[license-url]: https://github.com/twada/type-name/blob/master/LICENSE
[license-image]: https://img.shields.io/badge/license-MIT-brightgreen.svg
 
[saucelabs-url]: https://saucelabs.com/u/type-name
[saucelabs-image]: https://saucelabs.com/browser-matrix/type-name.svg