Function Name Retrieval Utility

3.0.0 · active · verified Sun Apr 19

get-func-name is a specialized utility library within the ChaiJS ecosystem, designed to securely and consistently retrieve the name of a JavaScript function. It offers a single, reliable API that works across both Node.js and browser environments, addressing inconsistencies that can arise from direct property access or varying runtime behaviors. The current stable version is `3.0.0`, which marks a definitive transition to being an ES Module (ESM) exclusively. This follows a previous, temporary ESM introduction and subsequent reversion in the v2.x series. The library maintains a focused release cadence driven by maintenance, bug fixes, and critical updates, such as the security patch in v2.0.1. Its primary differentiator is its cross-platform compatibility and robustness in extracting function names, making it a foundational tool for introspection in testing frameworks and other JavaScript libraries.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing `getFuncName` and using it to retrieve names from various function types including named, anonymous, arrow functions, and class methods.

import { getFuncName } from 'get-func-name';

// A function with an explicit name
function myNamedFunction() {
  // ...
}

// An anonymous function assigned to a variable
const myAnonymousFunction = function() {
  // ...
};

// An arrow function
const myArrowFunction = () => {
  // ...
};

// A function nested in an IIFE, which might not have an inferable name
const nestedAnonymous = (function() {
  return function() {};
}());

console.log(`Name of myNamedFunction: '${getFuncName(myNamedFunction)}'`);
console.log(`Name of myAnonymousFunction: '${getFuncName(myAnonymousFunction)}'`);
console.log(`Name of myArrowFunction: '${getFuncName(myArrowFunction)}'`);
console.log(`Name of nestedAnonymous: '${getFuncName(nestedAnonymous)}'`);

// Example with a class method
class MyClass {
  static staticMethod() {}
  instanceMethod() {}
}

console.log(`Name of staticMethod: '${getFuncName(MyClass.staticMethod)}'`);
console.log(`Name of instanceMethod: '${getFuncName(new MyClass().instanceMethod)}'`);

view raw JSON →