fn-args is a JavaScript utility that extracts parameter names from functions of any type, including regular functions, arrow functions, generator functions, and async functions. It uses advanced string parsing to handle complex parameter patterns like default values, rest parameters, and nested structures.
npm install fn-argsimport functionArguments from "fn-args";Note: This package is ESM-only and requires Node.js 12.20+ or 14.13.1+ or 16+. It cannot be imported with CommonJS require().
import functionArguments from "fn-args";
// Regular function
functionArguments(function (foo, bar) {});
//=> ['foo', 'bar']
// Arrow function
functionArguments((foo, bar) => {});
//=> ['foo', 'bar']
// Generator function
functionArguments(function* (foo, bar) {});
//=> ['foo', 'bar']
// Async function
functionArguments(async function (foo, bar) {});
//=> ['foo', 'bar']
// Function with default parameters
functionArguments((foo = 42, bar = 'hello') => {});
//=> ['foo', 'bar']
// Function with rest parameters
functionArguments((...args) => {});
//=> ['...args']Extracts parameter names from JavaScript functions using sophisticated string parsing that handles all function types and complex parameter patterns.
/**
* Get the parameter names of a function
* @param {Function} function_ - The function to extract parameter names from
* @returns {string[]} Array of parameter name strings
* @throws {TypeError} Throws "Expected a function" if input is not a function
*/
function functionArguments(function_: (...arguments: any[]) => any): string[];Supported Function Types:
function name() {}() => {}, param => {}, (param1, param2) => {}function* name() {}async function name() {}, async () => {}Parameter Pattern Support:
(foo, bar) => {}(foo = 42, bar = 'hello') => {}(...args) => {}, (first, ...rest) => {}(foo, bar,) => {}Advanced Features:
/* block */ and // inline comments in parameter listsUsage Examples:
import functionArguments from "fn-args";
// Complex default values
functionArguments((foo = {key: [1, 2, 3]}, bar = 'test') => {});
//=> ['foo', 'bar']
// Comments in parameters
functionArguments(function (
/* comment */ foo,
// another comment
bar
) {});
//=> ['foo', 'bar']
// Rest parameters mixed with regular parameters
functionArguments((first, second, ...rest) => {});
//=> ['first', 'second', '...rest']
// Single parameter arrow function
functionArguments(singleParam => {});
//=> ['singleParam']
// Empty parameter list
functionArguments(() => {});
//=> []
// Complex nested default values
functionArguments((config = {
nested: {
array: [1, 2, 3],
string: 'value with, commas'
}
}) => {});
//=> ['config']Error Handling:
// TypeError thrown for non-function inputs
try {
functionArguments("not a function");
} catch (error) {
console.log(error.message); // "Expected a function"
}