Skip to content

Commit

Permalink
Merge pull request #20 from lucasfcosta/return-null-for-non-function
Browse files Browse the repository at this point in the history
Return null when passed a non-function argument
  • Loading branch information
lucasfcosta committed Jan 17, 2017
2 parents addc96f + 19b8940 commit 17268c0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Expand Up @@ -10,6 +10,7 @@
* ### .getFuncName(constructorFn)
*
* Returns the name of a function.
* When a non-function instance is passed, returns `null`.
* This also includes a polyfill function if `aFunc.name` is not defined.
*
* @name getFuncName
Expand All @@ -21,6 +22,10 @@
var toString = Function.prototype.toString;
var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\s\(\/]+)/;
function getFuncName(aFunc) {
if (typeof aFunc !== 'function') {
return null;
}

var name = '';
if (typeof Function.prototype.name === 'undefined' && typeof aFunc.name === 'undefined') {
// Here we run a polyfill if Function does not support the `name` property and if aFunc.name is not defined
Expand Down
30 changes: 30 additions & 0 deletions test/index.js
Expand Up @@ -26,5 +26,35 @@ describe('getFuncName', function () {
}());
assert(getFuncName(anonymousFunc) === '');
});

it('should return `null` when passed a String as argument', function () {
assert(getFuncName('') === null);
});

it('should return `null` when passed a Number as argument', function () {
assert(getFuncName(1) === null);
});

it('should return `null` when passed a Boolean as argument', function () {
assert(getFuncName(true) === null);
});

it('should return `null` when passed `null` as argument', function () {
assert(getFuncName(null) === null);
});

it('should return `null` when passed `undefined` as argument', function () {
assert(getFuncName(undefined) === null);
});

it('should return `null` when passed a Symbol as argument', function () {
if (typeof Symbol !== 'undefined') {
assert(getFuncName(Symbol()) === null);
}
});

it('should return `null` when passed an Object as argument', function () {
assert(getFuncName({}) === null);
});
});

0 comments on commit 17268c0

Please sign in to comment.