You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.1 KiB

4 years ago
  1. 'use strict';
  2. var fnToStr = Function.prototype.toString;
  3. var constructorRegex = /^\s*class\b/;
  4. var isES6ClassFn = function isES6ClassFunction(value) {
  5. try {
  6. var fnStr = fnToStr.call(value);
  7. return constructorRegex.test(fnStr);
  8. } catch (e) {
  9. return false; // not a function
  10. }
  11. };
  12. var tryFunctionObject = function tryFunctionToStr(value) {
  13. try {
  14. if (isES6ClassFn(value)) { return false; }
  15. fnToStr.call(value);
  16. return true;
  17. } catch (e) {
  18. return false;
  19. }
  20. };
  21. var toStr = Object.prototype.toString;
  22. var fnClass = '[object Function]';
  23. var genClass = '[object GeneratorFunction]';
  24. var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
  25. module.exports = function isCallable(value) {
  26. if (!value) { return false; }
  27. if (typeof value !== 'function' && typeof value !== 'object') { return false; }
  28. if (typeof value === 'function' && !value.prototype) { return true; }
  29. if (hasToStringTag) { return tryFunctionObject(value); }
  30. if (isES6ClassFn(value)) { return false; }
  31. var strClass = toStr.call(value);
  32. return strClass === fnClass || strClass === genClass;
  33. };