1 | 'use strict';
|
---|
2 |
|
---|
3 | var whichBoxedPrimitive = require('which-boxed-primitive');
|
---|
4 | var whichCollection = require('which-collection');
|
---|
5 | var whichTypedArray = require('which-typed-array');
|
---|
6 | var isArray = require('isarray');
|
---|
7 | var isDate = require('is-date-object');
|
---|
8 | var isRegex = require('is-regex');
|
---|
9 | var isWeakRef = require('is-weakref');
|
---|
10 | var isFinalizationRegistry = require('is-finalizationregistry');
|
---|
11 | var name = require('function.prototype.name');
|
---|
12 | var isGeneratorFunction = require('is-generator-function');
|
---|
13 | var isAsyncFunction = require('is-async-function');
|
---|
14 | var hasToStringTag = require('has-tostringtag/shams')();
|
---|
15 | var toStringTag = hasToStringTag && Symbol.toStringTag;
|
---|
16 |
|
---|
17 | var $Object = Object;
|
---|
18 |
|
---|
19 | var promiseThen = typeof Promise === 'function' && Promise.prototype.then;
|
---|
20 | var isPromise = function isPromise(value) {
|
---|
21 | if (!value || typeof value !== 'object' || !promiseThen) {
|
---|
22 | return false;
|
---|
23 | }
|
---|
24 | try {
|
---|
25 | promiseThen.call(value, null, function () {});
|
---|
26 | return true;
|
---|
27 | } catch (e) {}
|
---|
28 | return false;
|
---|
29 | };
|
---|
30 |
|
---|
31 | var isKnownBuiltin = function isKnownBuiltin(builtinName) {
|
---|
32 | return builtinName
|
---|
33 | // primitives
|
---|
34 | && builtinName !== 'BigInt'
|
---|
35 | && builtinName !== 'Boolean'
|
---|
36 | && builtinName !== 'Null'
|
---|
37 | && builtinName !== 'Number'
|
---|
38 | && builtinName !== 'String'
|
---|
39 | && builtinName !== 'Symbol'
|
---|
40 | && builtinName !== 'Undefined'
|
---|
41 | // namespaces
|
---|
42 | && builtinName !== 'Math'
|
---|
43 | && builtinName !== 'JSON'
|
---|
44 | && builtinName !== 'Reflect'
|
---|
45 | && builtinName !== 'Atomics'
|
---|
46 | // collections
|
---|
47 | && builtinName !== 'Map'
|
---|
48 | && builtinName !== 'Set'
|
---|
49 | && builtinName !== 'WeakMap'
|
---|
50 | && builtinName !== 'WeakSet'
|
---|
51 | // typed arrays
|
---|
52 | && builtinName !== 'BigInt64Array'
|
---|
53 | && builtinName !== 'BigUint64Array'
|
---|
54 | && builtinName !== 'Float32Array'
|
---|
55 | && builtinName !== 'Float64Array'
|
---|
56 | && builtinName !== 'Int16Array'
|
---|
57 | && builtinName !== 'Int32Array'
|
---|
58 | && builtinName !== 'Int8Array'
|
---|
59 | && builtinName !== 'Uint16Array'
|
---|
60 | && builtinName !== 'Uint32Array'
|
---|
61 | && builtinName !== 'Uint8Array'
|
---|
62 | && builtinName !== 'Uint8ClampedArray'
|
---|
63 | // checked explicitly
|
---|
64 | && builtinName !== 'Array'
|
---|
65 | && builtinName !== 'Date'
|
---|
66 | && builtinName !== 'FinalizationRegistry'
|
---|
67 | && builtinName !== 'Promise'
|
---|
68 | && builtinName !== 'RegExp'
|
---|
69 | && builtinName !== 'WeakRef'
|
---|
70 | // functions
|
---|
71 | && builtinName !== 'Function'
|
---|
72 | && builtinName !== 'GeneratorFunction'
|
---|
73 | && builtinName !== 'AsyncFunction';
|
---|
74 | };
|
---|
75 |
|
---|
76 | module.exports = function whichBuiltinType(value) {
|
---|
77 | if (value == null) {
|
---|
78 | return value;
|
---|
79 | }
|
---|
80 | // covers: primitives, {,Weak}Map/Set, typed arrays
|
---|
81 | var which = whichBoxedPrimitive($Object(value)) || whichCollection(value) || whichTypedArray(value);
|
---|
82 | if (which) {
|
---|
83 | return which;
|
---|
84 | }
|
---|
85 | if (isArray(value)) {
|
---|
86 | return 'Array';
|
---|
87 | }
|
---|
88 | if (isDate(value)) {
|
---|
89 | return 'Date';
|
---|
90 | }
|
---|
91 | if (isRegex(value)) {
|
---|
92 | return 'RegExp';
|
---|
93 | }
|
---|
94 | if (isWeakRef(value)) {
|
---|
95 | return 'WeakRef';
|
---|
96 | }
|
---|
97 | if (isFinalizationRegistry(value)) {
|
---|
98 | return 'FinalizationRegistry';
|
---|
99 | }
|
---|
100 | if (typeof value === 'function') {
|
---|
101 | if (isGeneratorFunction(value)) {
|
---|
102 | return 'GeneratorFunction';
|
---|
103 | }
|
---|
104 | if (isAsyncFunction(value)) {
|
---|
105 | return 'AsyncFunction';
|
---|
106 | }
|
---|
107 | return 'Function';
|
---|
108 | }
|
---|
109 | if (isPromise(value)) {
|
---|
110 | return 'Promise';
|
---|
111 | }
|
---|
112 | if (toStringTag && toStringTag in value) {
|
---|
113 | var tag = value[toStringTag];
|
---|
114 | if (isKnownBuiltin(tag)) {
|
---|
115 | return tag;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | if (typeof value.constructor === 'function') {
|
---|
119 | var constructorName = name(value.constructor);
|
---|
120 | if (isKnownBuiltin(constructorName)) {
|
---|
121 | return constructorName;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return 'Object';
|
---|
125 | };
|
---|