UNPKG

1.33 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var uncurryThis = require('../internals/function-uncurry-this');
4var $isCallable = require('../internals/is-callable');
5var inspectSource = require('../internals/inspect-source');
6var hasOwn = require('../internals/has-own-property');
7var DESCRIPTORS = require('../internals/descriptors');
8
9// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
10var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
11var classRegExp = /^\s*class\b/;
12var exec = uncurryThis(classRegExp.exec);
13
14var isClassConstructor = function (argument) {
15 try {
16 // `Function#toString` throws on some built-it function in some legacy engines
17 // (for example, `DOMQuad` and similar in FF41-)
18 if (!DESCRIPTORS || !exec(classRegExp, inspectSource(argument))) return false;
19 } catch (error) { /* empty */ }
20 var prototype = getOwnPropertyDescriptor(argument, 'prototype');
21 return !!prototype && hasOwn(prototype, 'writable') && !prototype.writable;
22};
23
24// `Function.isCallable` method
25// https://github.com/caitp/TC39-Proposals/blob/trunk/tc39-reflect-isconstructor-iscallable.md
26$({ target: 'Function', stat: true, sham: true, forced: true }, {
27 isCallable: function isCallable(argument) {
28 return $isCallable(argument) && !isClassConstructor(argument);
29 }
30});