UNPKG

3.22 kBJavaScriptView Raw
1'use strict';
2
3var fnToStr = Function.prototype.toString;
4var reflectApply = typeof Reflect === 'object' && Reflect !== null && Reflect.apply;
5var badArrayLike;
6var isCallableMarker;
7if (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
26var constructorRegex = /^\s*class\b/;
27var 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
36var 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};
45var toStr = Object.prototype.toString;
46var objectClass = '[object Object]';
47var fnClass = '[object Function]';
48var genClass = '[object GeneratorFunction]';
49var ddaClass = '[object HTMLAllCollection]'; // IE 11
50var ddaClass2 = '[object HTML document.all class]';
51var ddaClass3 = '[object HTMLCollection]'; // IE 9-10
52var hasToStringTag = typeof Symbol === 'function' && !!Symbol.toStringTag; // better: use `has-tostringtag`
53
54var isIE68 = !(0 in [,]); // eslint-disable-line no-sparse-arrays, comma-spacing
55
56var isDDA = function isDocumentDotAll() { return false; };
57if (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
80module.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 };