Skip to content

Latest commit

 

History

History
120 lines (84 loc) · 2.19 KB

File metadata and controls

120 lines (84 loc) · 2.19 KB

Get Parameter Names

Parse a function and extract its parameter names.

Based on get-parameter-names (MIT).

Changelog

Installation

Using npm:

npm install @slimlib/get-parameter-names

Using pnpm:

pnpm add @slimlib/get-parameter-names

Usage

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']

API

getParameterNames(input)

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

Features

  • 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

Examples

Regular Functions

function add(a, b, c) {
  return a + b + c;
}
getParameterNames(add); // ['a', 'b', 'c']

Arrow Functions

const multiply = (x, y) => x * y;
getParameterNames(multiply); // ['x', 'y']

const square = (x) => x * x;
getParameterNames(square); // ['x']

Async Functions

async function fetchData(url, options) {
  return await fetch(url, options);
}
getParameterNames(fetchData); // ['url', 'options']

Default Parameters

function greet(name, greeting = "Hello") {
  return `${greeting}, ${name}!`;
}
getParameterNames(greet); // ['name', 'greeting']

Class Constructors

class User {
  constructor(name, email, age) {
    this.name = name;
    this.email = email;
    this.age = age;
  }
}
getParameterNames(User); // ['name', 'email', 'age']

Function Strings

const fnString = "(a, b) => a + b";
getParameterNames(fnString); // ['a', 'b']

License

MIT