Skip to content

Commit c86b2dc

Browse files
authored
Merge pull request #12 from lucasfcosta/external-get-func-name
External get func name
2 parents b48711f + ab38c4c commit c86b2dc

File tree

2 files changed

+9
-31
lines changed

2 files changed

+9
-31
lines changed

index.js

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* MIT Licensed
77
*/
88

9+
var getFunctionName = require('get-func-name');
910
/**
1011
* ### .checkError
1112
*
@@ -85,34 +86,6 @@ function compatibleMessage(thrown, errMatcher) {
8586
return false;
8687
}
8788

88-
/**
89-
* ### .getFunctionName(constructorFn)
90-
*
91-
* Returns the name of a function.
92-
* This also includes a polyfill function if `constructorFn.name` is not defined.
93-
*
94-
* @name getFunctionName
95-
* @param {Function} constructorFn
96-
* @namespace Utils
97-
* @api private
98-
*/
99-
100-
var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\(\/]+)/;
101-
function getFunctionName(constructorFn) {
102-
var name = '';
103-
if (typeof constructorFn.name === 'undefined') {
104-
// Here we run a polyfill if constructorFn.name is not defined
105-
var match = String(constructorFn).match(functionNameMatch);
106-
if (match) {
107-
name = match[1];
108-
}
109-
} else {
110-
name = constructorFn.name;
111-
}
112-
113-
return name;
114-
}
115-
11689
/**
11790
* ### .getConstructorName(errorLike)
11891
*
@@ -132,8 +105,11 @@ function getConstructorName(errorLike) {
132105
// If `err` is not an instance of Error it is an error constructor itself or another function.
133106
// If we've got a common function we get its name, otherwise we may need to create a new instance
134107
// of the error just in case it's a poorly-constructed error. Please see chaijs/chai/issues/45 to know more.
135-
constructorName = getFunctionName(errorLike).trim() ||
136-
getFunctionName(new errorLike()); // eslint-disable-line new-cap
108+
constructorName = getFunctionName(errorLike);
109+
if (constructorName === '') {
110+
var newConstructorName = getFunctionName(new errorLike()); // eslint-disable-line new-cap
111+
constructorName = newConstructorName || constructorName;
112+
}
137113
}
138114

139115
return constructorName;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
"max-statements": 0
5555
}
5656
},
57-
"dependencies": {},
57+
"dependencies": {
58+
"get-func-name": "^1.0.0"
59+
},
5860
"devDependencies": {
5961
"browserify": "^13.0.0",
6062
"browserify-istanbul": "^1.0.0",

0 commit comments

Comments
 (0)