Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function-name - Get the name of a named function
/*!
function-name
Get the name of a named function
https://github.com/sindresorhus/function-name
by Sindre Sorhus
MIT License
*/
function functionName(fn) {
if (typeof fn !== 'function') {
throw new TypeError('Expected a function.');
}
return fn.name || (/function ([^\(]+)/.exec(fn.toString()) || [])[1] || null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment