12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- 'use strict';
- var IsCallable = require('es-abstract/2021/IsCallable');
- var functionsHaveNames = require('functions-have-names')();
- var callBound = require('call-bind/callBound');
- var $functionToString = callBound('Function.prototype.toString');
- var $stringMatch = callBound('String.prototype.match');
- var classRegex = /^class /;
- var isClass = function isClassConstructor(fn) {
- if (IsCallable(fn)) {
- return false;
- }
- if (typeof fn !== 'function') {
- return false;
- }
- try {
- var match = $stringMatch($functionToString(fn), classRegex);
- return !!match;
- } catch (e) {}
- return false;
- };
- var regex = /\s*function\s+([^(\s]*)\s*/;
- var functionProto = Function.prototype;
- module.exports = function getName() {
- if (!isClass(this) && !IsCallable(this)) {
- throw new TypeError('Function.prototype.name sham getter called on non-function');
- }
- if (functionsHaveNames) {
- return this.name;
- }
- if (this === functionProto) {
- return '';
- }
- var str = $functionToString(this);
- var match = $stringMatch(str, regex);
- var name = match && match[1];
- return name;
- };
|