1 | 'use strict';
|
2 | var uncurryThis = require('../internals/function-uncurry-this');
|
3 | var fails = require('../internals/fails');
|
4 | var isCallable = require('../internals/is-callable');
|
5 | var classof = require('../internals/classof');
|
6 | var getBuiltIn = require('../internals/get-built-in');
|
7 | var inspectSource = require('../internals/inspect-source');
|
8 |
|
9 | var noop = function () { };
|
10 | var construct = getBuiltIn('Reflect', 'construct');
|
11 | var constructorRegExp = /^\s*(?:class|function)\b/;
|
12 | var exec = uncurryThis(constructorRegExp.exec);
|
13 | var INCORRECT_TO_STRING = !constructorRegExp.test(noop);
|
14 |
|
15 | var isConstructorModern = function isConstructor(argument) {
|
16 | if (!isCallable(argument)) return false;
|
17 | try {
|
18 | construct(noop, [], argument);
|
19 | return true;
|
20 | } catch (error) {
|
21 | return false;
|
22 | }
|
23 | };
|
24 |
|
25 | var isConstructorLegacy = function isConstructor(argument) {
|
26 | if (!isCallable(argument)) return false;
|
27 | switch (classof(argument)) {
|
28 | case 'AsyncFunction':
|
29 | case 'GeneratorFunction':
|
30 | case 'AsyncGeneratorFunction': return false;
|
31 | }
|
32 | try {
|
33 |
|
34 |
|
35 |
|
36 | return INCORRECT_TO_STRING || !!exec(constructorRegExp, inspectSource(argument));
|
37 | } catch (error) {
|
38 | return true;
|
39 | }
|
40 | };
|
41 |
|
42 | isConstructorLegacy.sham = true;
|
43 |
|
44 |
|
45 |
|
46 | module.exports = !construct || fails(function () {
|
47 | var called;
|
48 | return isConstructorModern(isConstructorModern.call)
|
49 | || !isConstructorModern(Object)
|
50 | || !isConstructorModern(function () { called = true; })
|
51 | || called;
|
52 | }) ? isConstructorLegacy : isConstructorModern;
|