2020-08-26 01:57:08 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var fnToStr = Function.prototype.toString;
|
|
|
|
var reflectApply = typeof Reflect === 'object' && Reflect !== null && Reflect.apply;
|
|
|
|
var badArrayLike;
|
|
|
|
var isCallableMarker;
|
|
|
|
if (typeof reflectApply === 'function' && typeof Object.defineProperty === 'function') {
|
|
|
|
try {
|
|
|
|
badArrayLike = Object.defineProperty({}, 'length', {
|
|
|
|
get: function () {
|
|
|
|
throw isCallableMarker;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
isCallableMarker = {};
|
2021-02-26 04:58:33 +01:00
|
|
|
// eslint-disable-next-line no-throw-literal
|
|
|
|
reflectApply(function () { throw 42; }, null, badArrayLike);
|
2020-08-26 01:57:08 +02:00
|
|
|
} catch (_) {
|
2021-02-26 04:58:33 +01:00
|
|
|
if (_ !== isCallableMarker) {
|
|
|
|
reflectApply = null;
|
|
|
|
}
|
2020-08-26 01:57:08 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reflectApply = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var constructorRegex = /^\s*class\b/;
|
|
|
|
var isES6ClassFn = function isES6ClassFunction(value) {
|
|
|
|
try {
|
|
|
|
var fnStr = fnToStr.call(value);
|
|
|
|
return constructorRegex.test(fnStr);
|
|
|
|
} catch (e) {
|
|
|
|
return false; // not a function
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var tryFunctionObject = function tryFunctionToStr(value) {
|
|
|
|
try {
|
|
|
|
if (isES6ClassFn(value)) { return false; }
|
|
|
|
fnToStr.call(value);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var toStr = Object.prototype.toString;
|
2022-11-10 11:43:16 +01:00
|
|
|
var objectClass = '[object Object]';
|
2020-08-26 01:57:08 +02:00
|
|
|
var fnClass = '[object Function]';
|
|
|
|
var genClass = '[object GeneratorFunction]';
|
2022-11-10 11:43:16 +01:00
|
|
|
var ddaClass = '[object HTMLAllCollection]'; // IE 11
|
|
|
|
var ddaClass2 = '[object HTML document.all class]';
|
|
|
|
var ddaClass3 = '[object HTMLCollection]'; // IE 9-10
|
|
|
|
var hasToStringTag = typeof Symbol === 'function' && !!Symbol.toStringTag; // better: use `has-tostringtag`
|
|
|
|
|
|
|
|
var isIE68 = !(0 in [,]); // eslint-disable-line no-sparse-arrays, comma-spacing
|
|
|
|
|
|
|
|
var isDDA = function isDocumentDotAll() { return false; };
|
|
|
|
if (typeof document === 'object') {
|
|
|
|
// Firefox 3 canonicalizes DDA to undefined when it's not accessed directly
|
|
|
|
var all = document.all;
|
|
|
|
if (toStr.call(all) === toStr.call(document.all)) {
|
|
|
|
isDDA = function isDocumentDotAll(value) {
|
|
|
|
/* globals document: false */
|
|
|
|
// in IE 6-8, typeof document.all is "object" and it's truthy
|
|
|
|
if ((isIE68 || !value) && (typeof value === 'undefined' || typeof value === 'object')) {
|
|
|
|
try {
|
|
|
|
var str = toStr.call(value);
|
|
|
|
return (
|
|
|
|
str === ddaClass
|
|
|
|
|| str === ddaClass2
|
|
|
|
|| str === ddaClass3 // opera 12.16
|
|
|
|
|| str === objectClass // IE 6-8
|
|
|
|
) && value('') == null; // eslint-disable-line eqeqeq
|
|
|
|
} catch (e) { /**/ }
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
module.exports = reflectApply
|
|
|
|
? function isCallable(value) {
|
2022-11-10 11:43:16 +01:00
|
|
|
if (isDDA(value)) { return true; }
|
2020-08-26 01:57:08 +02:00
|
|
|
if (!value) { return false; }
|
|
|
|
if (typeof value !== 'function' && typeof value !== 'object') { return false; }
|
|
|
|
try {
|
|
|
|
reflectApply(value, null, badArrayLike);
|
|
|
|
} catch (e) {
|
|
|
|
if (e !== isCallableMarker) { return false; }
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
return !isES6ClassFn(value) && tryFunctionObject(value);
|
2020-08-26 01:57:08 +02:00
|
|
|
}
|
|
|
|
: function isCallable(value) {
|
2022-11-10 11:43:16 +01:00
|
|
|
if (isDDA(value)) { return true; }
|
2020-08-26 01:57:08 +02:00
|
|
|
if (!value) { return false; }
|
|
|
|
if (typeof value !== 'function' && typeof value !== 'object') { return false; }
|
|
|
|
if (hasToStringTag) { return tryFunctionObject(value); }
|
|
|
|
if (isES6ClassFn(value)) { return false; }
|
|
|
|
var strClass = toStr.call(value);
|
2022-11-10 11:43:16 +01:00
|
|
|
if (strClass !== fnClass && strClass !== genClass && !(/^\[object HTML/).test(strClass)) { return false; }
|
|
|
|
return tryFunctionObject(value);
|
2020-08-26 01:57:08 +02:00
|
|
|
};
|