Parse a function and extract its parameter names.
Based on get-parameter-names (MIT).
Using npm:
npm install @slimlib/get-parameter-names
Using pnpm:
pnpm add @slimlib/get-parameter-names
import { getParameterNames } from "@slimlib/get-parameter-names";
function example(foo, bar, baz) {
return foo + bar + baz;
}
const params = getParameterNames(example);
console.log(params); // ['foo', 'bar', 'baz']Parse a function and extract its parameter names.
Parameters:
input-Function | string- The function or function string to parse
Returns: string[] - Array of parameter names
- Supports regular functions
- Supports ES2015+ arrow functions (with and without parentheses)
- Supports async functions
- Supports default parameters
- Supports class constructors
- Handles comments in function signatures
- Handles nested functions and arrow functions
- Works with functions created using the Function constructor
function add(a, b, c) {
return a + b + c;
}
getParameterNames(add); // ['a', 'b', 'c']const multiply = (x, y) => x * y;
getParameterNames(multiply); // ['x', 'y']
const square = (x) => x * x;
getParameterNames(square); // ['x']async function fetchData(url, options) {
return await fetch(url, options);
}
getParameterNames(fetchData); // ['url', 'options']function greet(name, greeting = "Hello") {
return `${greeting}, ${name}!`;
}
getParameterNames(greet); // ['name', 'greeting']class User {
constructor(name, email, age) {
this.name = name;
this.email = email;
this.age = age;
}
}
getParameterNames(User); // ['name', 'email', 'age']const fnString = "(a, b) => a + b";
getParameterNames(fnString); // ['a', 'b']