index.js 3.1 KB

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