function-name.js 818 B

1234567891011121314151617181920212223242526272829
  1. "use strict";
  2. /**
  3. * Returns a display name for a function
  4. *
  5. * @param {Function} func
  6. * @returns {string}
  7. */
  8. module.exports = function functionName(func) {
  9. if (!func) {
  10. return "";
  11. }
  12. try {
  13. return (
  14. func.displayName ||
  15. func.name ||
  16. // Use function decomposition as a last resort to get function
  17. // name. Does not rely on function decomposition to work - if it
  18. // doesn't debugging will be slightly less informative
  19. // (i.e. toString will say 'spy' rather than 'myFunc').
  20. (String(func).match(/function ([^\s(]+)/) || [])[1]
  21. );
  22. } catch (e) {
  23. // Stringify may fail and we might get an exception, as a last-last
  24. // resort fall back to empty string.
  25. return "";
  26. }
  27. };