123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 'use strict';
- var whichBoxedPrimitive = require('which-boxed-primitive');
- var whichCollection = require('which-collection');
- var whichTypedArray = require('which-typed-array');
- var isArray = require('isarray');
- var isDate = require('is-date-object');
- var isRegex = require('is-regex');
- var isWeakRef = require('is-weakref');
- var isFinalizationRegistry = require('is-finalizationregistry');
- var name = require('function.prototype.name');
- var isGeneratorFunction = require('is-generator-function');
- var isAsyncFunction = require('is-async-function');
- var hasToStringTag = require('has-tostringtag/shams')();
- var toStringTag = hasToStringTag && Symbol.toStringTag;
- var $Object = Object;
- var promiseThen = typeof Promise === 'function' && Promise.prototype.then;
- var isPromise = function isPromise(value) {
- if (!value || typeof value !== 'object' || !promiseThen) {
- return false;
- }
- try {
- promiseThen.call(value, null, function () {});
- return true;
- } catch (e) {}
- return false;
- };
- var isKnownBuiltin = function isKnownBuiltin(builtinName) {
- return builtinName
- // primitives
- && builtinName !== 'BigInt'
- && builtinName !== 'Boolean'
- && builtinName !== 'Null'
- && builtinName !== 'Number'
- && builtinName !== 'String'
- && builtinName !== 'Symbol'
- && builtinName !== 'Undefined'
- // namespaces
- && builtinName !== 'Math'
- && builtinName !== 'JSON'
- && builtinName !== 'Reflect'
- && builtinName !== 'Atomics'
- // collections
- && builtinName !== 'Map'
- && builtinName !== 'Set'
- && builtinName !== 'WeakMap'
- && builtinName !== 'WeakSet'
- // typed arrays
- && builtinName !== 'BigInt64Array'
- && builtinName !== 'BigUint64Array'
- && builtinName !== 'Float32Array'
- && builtinName !== 'Float64Array'
- && builtinName !== 'Int16Array'
- && builtinName !== 'Int32Array'
- && builtinName !== 'Int8Array'
- && builtinName !== 'Uint16Array'
- && builtinName !== 'Uint32Array'
- && builtinName !== 'Uint8Array'
- && builtinName !== 'Uint8ClampedArray'
- // checked explicitly
- && builtinName !== 'Array'
- && builtinName !== 'Date'
- && builtinName !== 'FinalizationRegistry'
- && builtinName !== 'Promise'
- && builtinName !== 'RegExp'
- && builtinName !== 'WeakRef'
- // functions
- && builtinName !== 'Function'
- && builtinName !== 'GeneratorFunction'
- && builtinName !== 'AsyncFunction';
- };
- module.exports = function whichBuiltinType(value) {
- if (value == null) {
- return value;
- }
- // covers: primitives, {,Weak}Map/Set, typed arrays
- var which = whichBoxedPrimitive($Object(value)) || whichCollection(value) || whichTypedArray(value);
- if (which) {
- return which;
- }
- if (isArray(value)) {
- return 'Array';
- }
- if (isDate(value)) {
- return 'Date';
- }
- if (isRegex(value)) {
- return 'RegExp';
- }
- if (isWeakRef(value)) {
- return 'WeakRef';
- }
- if (isFinalizationRegistry(value)) {
- return 'FinalizationRegistry';
- }
- if (typeof value === 'function') {
- if (isGeneratorFunction(value)) {
- return 'GeneratorFunction';
- }
- if (isAsyncFunction(value)) {
- return 'AsyncFunction';
- }
- return 'Function';
- }
- if (isPromise(value)) {
- return 'Promise';
- }
- if (toStringTag && toStringTag in value) {
- var tag = value[toStringTag];
- if (isKnownBuiltin(tag)) {
- return tag;
- }
- }
- if (typeof value.constructor === 'function') {
- var constructorName = name(value.constructor);
- if (isKnownBuiltin(constructorName)) {
- return constructorName;
- }
- }
- return 'Object';
- };
|