Permalink
Cannot retrieve contributors at this time
43 lines (37 sloc)
1.05 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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; | |
| }; |