From 19b89404bdf1909c0d1d46e00b33008d98190a3c Mon Sep 17 00:00:00 2001 From: lucasfcosta Date: Wed, 11 Jan 2017 17:10:33 -0200 Subject: [PATCH] chore: return null when passed a non-function argument --- index.js | 5 +++++ test/index.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/index.js b/index.js index 8cc5c38..e4aa582 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 diff --git a/test/index.js b/test/index.js index b2314ae..240a5ad 100644 --- a/test/index.js +++ b/test/index.js @@ -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); + }); });