or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

fn-args

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.

Package Information

  • Package Name: fn-args
  • Package Type: npm
  • Language: JavaScript (ES Modules)
  • Installation: npm install fn-args

Core Imports

import 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().

Basic Usage

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

Capabilities

Function Parameter Extraction

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:

  • Regular functions: function name() {}
  • Arrow functions: () => {}, param => {}, (param1, param2) => {}
  • Generator functions: function* name() {}
  • Async functions: async function name() {}, async () => {}

Parameter Pattern Support:

  • Simple parameters: (foo, bar) => {}
  • Default parameters: (foo = 42, bar = 'hello') => {}
  • Rest parameters: (...args) => {}, (first, ...rest) => {}
  • Complex default values: Objects, arrays, nested structures, regex literals
  • Parameters with comments: Strips inline and block comments
  • Trailing commas: (foo, bar,) => {}

Advanced Features:

  • Default Value Stripping: Removes complex default parameter values while preserving parameter names
  • Comment Removal: Handles both /* block */ and // inline comments in parameter lists
  • Quote Handling: Properly processes string literals with single, double, and template literal quotes
  • Nested Structure Parsing: Correctly handles nested parentheses, brackets, and braces in default values
  • Edge Case Support: Empty parameter lists, single parameters without parentheses, complex regex patterns

Usage 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"
}