-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
implementation.js
43 lines (37 loc) · 1.04 KB
/
implementation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
var isCallable = require('is-callable');
var functionsHaveNames = require('./helpers/functionsHaveNames');
var bind = require('function-bind');
var functionToString = bind.call(Function.call, Function.prototype.toString);
var stringMatch = bind.call(Function.call, 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;
};