[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var fnToStr = Function.prototype.toString;
|
---|
| 4 | var reflectApply = typeof Reflect === 'object' && Reflect !== null && Reflect.apply;
|
---|
| 5 | var badArrayLike;
|
---|
| 6 | var isCallableMarker;
|
---|
| 7 | if (typeof reflectApply === 'function' && typeof Object.defineProperty === 'function') {
|
---|
| 8 | try {
|
---|
| 9 | badArrayLike = Object.defineProperty({}, 'length', {
|
---|
| 10 | get: function () {
|
---|
| 11 | throw isCallableMarker;
|
---|
| 12 | }
|
---|
| 13 | });
|
---|
| 14 | isCallableMarker = {};
|
---|
| 15 | // eslint-disable-next-line no-throw-literal
|
---|
| 16 | reflectApply(function () { throw 42; }, null, badArrayLike);
|
---|
| 17 | } catch (_) {
|
---|
| 18 | if (_ !== isCallableMarker) {
|
---|
| 19 | reflectApply = null;
|
---|
| 20 | }
|
---|
| 21 | }
|
---|
| 22 | } else {
|
---|
| 23 | reflectApply = null;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | var constructorRegex = /^\s*class\b/;
|
---|
| 27 | var isES6ClassFn = function isES6ClassFunction(value) {
|
---|
| 28 | try {
|
---|
| 29 | var fnStr = fnToStr.call(value);
|
---|
| 30 | return constructorRegex.test(fnStr);
|
---|
| 31 | } catch (e) {
|
---|
| 32 | return false; // not a function
|
---|
| 33 | }
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | var tryFunctionObject = function tryFunctionToStr(value) {
|
---|
| 37 | try {
|
---|
| 38 | if (isES6ClassFn(value)) { return false; }
|
---|
| 39 | fnToStr.call(value);
|
---|
| 40 | return true;
|
---|
| 41 | } catch (e) {
|
---|
| 42 | return false;
|
---|
| 43 | }
|
---|
| 44 | };
|
---|
| 45 | var toStr = Object.prototype.toString;
|
---|
| 46 | var objectClass = '[object Object]';
|
---|
| 47 | var fnClass = '[object Function]';
|
---|
| 48 | var genClass = '[object GeneratorFunction]';
|
---|
| 49 | var ddaClass = '[object HTMLAllCollection]'; // IE 11
|
---|
| 50 | var ddaClass2 = '[object HTML document.all class]';
|
---|
| 51 | var ddaClass3 = '[object HTMLCollection]'; // IE 9-10
|
---|
| 52 | var hasToStringTag = typeof Symbol === 'function' && !!Symbol.toStringTag; // better: use `has-tostringtag`
|
---|
| 53 |
|
---|
| 54 | var isIE68 = !(0 in [,]); // eslint-disable-line no-sparse-arrays, comma-spacing
|
---|
| 55 |
|
---|
| 56 | var isDDA = function isDocumentDotAll() { return false; };
|
---|
| 57 | if (typeof document === 'object') {
|
---|
| 58 | // Firefox 3 canonicalizes DDA to undefined when it's not accessed directly
|
---|
| 59 | var all = document.all;
|
---|
| 60 | if (toStr.call(all) === toStr.call(document.all)) {
|
---|
| 61 | isDDA = function isDocumentDotAll(value) {
|
---|
| 62 | /* globals document: false */
|
---|
| 63 | // in IE 6-8, typeof document.all is "object" and it's truthy
|
---|
| 64 | if ((isIE68 || !value) && (typeof value === 'undefined' || typeof value === 'object')) {
|
---|
| 65 | try {
|
---|
| 66 | var str = toStr.call(value);
|
---|
| 67 | return (
|
---|
| 68 | str === ddaClass
|
---|
| 69 | || str === ddaClass2
|
---|
| 70 | || str === ddaClass3 // opera 12.16
|
---|
| 71 | || str === objectClass // IE 6-8
|
---|
| 72 | ) && value('') == null; // eslint-disable-line eqeqeq
|
---|
| 73 | } catch (e) { /**/ }
|
---|
| 74 | }
|
---|
| 75 | return false;
|
---|
| 76 | };
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | module.exports = reflectApply
|
---|
| 81 | ? function isCallable(value) {
|
---|
| 82 | if (isDDA(value)) { return true; }
|
---|
| 83 | if (!value) { return false; }
|
---|
| 84 | if (typeof value !== 'function' && typeof value !== 'object') { return false; }
|
---|
| 85 | try {
|
---|
| 86 | reflectApply(value, null, badArrayLike);
|
---|
| 87 | } catch (e) {
|
---|
| 88 | if (e !== isCallableMarker) { return false; }
|
---|
| 89 | }
|
---|
| 90 | return !isES6ClassFn(value) && tryFunctionObject(value);
|
---|
| 91 | }
|
---|
| 92 | : function isCallable(value) {
|
---|
| 93 | if (isDDA(value)) { return true; }
|
---|
| 94 | if (!value) { return false; }
|
---|
| 95 | if (typeof value !== 'function' && typeof value !== 'object') { return false; }
|
---|
| 96 | if (hasToStringTag) { return tryFunctionObject(value); }
|
---|
| 97 | if (isES6ClassFn(value)) { return false; }
|
---|
| 98 | var strClass = toStr.call(value);
|
---|
| 99 | if (strClass !== fnClass && strClass !== genClass && !(/^\[object HTML/).test(strClass)) { return false; }
|
---|
| 100 | return tryFunctionObject(value);
|
---|
| 101 | };
|
---|