1 | 'use strict';
|
---|
2 |
|
---|
3 | var IsCallable = require('es-abstract/2023/IsCallable');
|
---|
4 | var HasOwnProperty = require('es-abstract/2023/HasOwnProperty');
|
---|
5 | var functionsHaveNames = require('functions-have-names')();
|
---|
6 | var callBound = require('call-bind/callBound');
|
---|
7 | var $functionToString = callBound('Function.prototype.toString');
|
---|
8 | var $stringMatch = callBound('String.prototype.match');
|
---|
9 | var toStr = callBound('Object.prototype.toString');
|
---|
10 |
|
---|
11 | var classRegex = /^class /;
|
---|
12 |
|
---|
13 | var isClass = function isClassConstructor(fn) {
|
---|
14 | if (IsCallable(fn)) {
|
---|
15 | return false;
|
---|
16 | }
|
---|
17 | if (typeof fn !== 'function') {
|
---|
18 | return false;
|
---|
19 | }
|
---|
20 | try {
|
---|
21 | var match = $stringMatch($functionToString(fn), classRegex);
|
---|
22 | return !!match;
|
---|
23 | } catch (e) {}
|
---|
24 | return false;
|
---|
25 | };
|
---|
26 |
|
---|
27 | var regex = /\s*function\s+([^(\s]*)\s*/;
|
---|
28 |
|
---|
29 | var isIE68 = !(0 in [,]); // eslint-disable-line no-sparse-arrays, comma-spacing
|
---|
30 |
|
---|
31 | var objectClass = '[object Object]';
|
---|
32 | var ddaClass = '[object HTMLAllCollection]';
|
---|
33 |
|
---|
34 | var functionProto = Function.prototype;
|
---|
35 |
|
---|
36 | var isDDA = function isDocumentDotAll() {
|
---|
37 | return false;
|
---|
38 | };
|
---|
39 | if (typeof document === 'object') {
|
---|
40 | // Firefox 3 canonicalizes DDA to undefined when it's not accessed directly
|
---|
41 | var all = document.all;
|
---|
42 | if (toStr(all) === toStr(document.all)) {
|
---|
43 | isDDA = function isDocumentDotAll(value) {
|
---|
44 | /* globals document: false */
|
---|
45 | // in IE 6-8, typeof document.all is "object" and it's truthy
|
---|
46 | if ((isIE68 || !value) && (typeof value === 'undefined' || typeof value === 'object')) {
|
---|
47 | try {
|
---|
48 | var str = toStr(value);
|
---|
49 | // IE 6-8 uses `objectClass`
|
---|
50 | return (str === ddaClass || str === objectClass) && value('') == null; // eslint-disable-line eqeqeq
|
---|
51 | } catch (e) { /**/ }
|
---|
52 | }
|
---|
53 | return false;
|
---|
54 | };
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | module.exports = function getName() {
|
---|
59 | if (isDDA(this) || (!isClass(this) && !IsCallable(this))) {
|
---|
60 | throw new TypeError('Function.prototype.name sham getter called on non-function');
|
---|
61 | }
|
---|
62 | if (functionsHaveNames && HasOwnProperty(this, 'name')) {
|
---|
63 | return this.name;
|
---|
64 | }
|
---|
65 | if (this === functionProto) {
|
---|
66 | return '';
|
---|
67 | }
|
---|
68 | var str = $functionToString(this);
|
---|
69 | var match = $stringMatch(str, regex);
|
---|
70 | var name = match && match[1];
|
---|
71 | return name;
|
---|
72 | };
|
---|