index.js 860 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. var callBound = require('call-bound');
  3. var safeRegexTest = require('safe-regex-test');
  4. var isFnRegex = safeRegexTest(/^\s*(?:function)?\*/);
  5. var hasToStringTag = require('has-tostringtag/shams')();
  6. var getProto = require('get-proto');
  7. var toStr = callBound('Object.prototype.toString');
  8. var fnToStr = callBound('Function.prototype.toString');
  9. var getGeneratorFunction = require('generator-function');
  10. /** @type {import('.')} */
  11. module.exports = function isGeneratorFunction(fn) {
  12. if (typeof fn !== 'function') {
  13. return false;
  14. }
  15. if (isFnRegex(fnToStr(fn))) {
  16. return true;
  17. }
  18. if (!hasToStringTag) {
  19. var str = toStr(fn);
  20. return str === '[object GeneratorFunction]';
  21. }
  22. if (!getProto) {
  23. return false;
  24. }
  25. var GeneratorFunction = getGeneratorFunction();
  26. return GeneratorFunction && getProto(fn) === GeneratorFunction.prototype;
  27. };