-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
42 lines (37 loc) · 903 Bytes
/
index.js
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
/**
* type-name - Just a reasonable typeof
*
* https://github.com/twada/type-name
*
* Copyright (c) 2014-2021 Takuto Wada
* Licensed under the MIT license.
* https://github.com/twada/type-name/blob/master/LICENSE
*/
'use strict';
var toStr = Object.prototype.toString;
function funcName (f) {
if (f.name) {
return f.name;
}
var match = /^\s*function\s*([^\(\s]+)/i.exec(f.toString());
return match ? match[1] : '';
}
function ctorName (obj) {
var strName = toStr.call(obj).slice(8, -1);
if ((strName === 'Object' || strName === 'Error') && obj.constructor) {
return funcName(obj.constructor);
}
return strName;
}
function typeName (val) {
var type;
if (val === null) {
return 'null';
}
type = typeof val;
if (type === 'object') {
return ctorName(val);
}
return type;
}
module.exports = typeName;