npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

function-descriptor

v1.1.1

Published

A Javascript library for inspecting a function's signature, including parameter names.

Downloads

13

Readme

Javascript FunctionDescriptors

Fast, foolproof library to describe a Javascript function, including its parameter names and traits.

This library uses acorn to parse the stringified function, then traverses the syntax tree. Because it doesn't rely on Regex, it is much less brittle than other libraries.

const describeFunction = require('function-descriptor')

const fn = (a, b=1) => a + b
const descriptor = describeFunction(fn)

console.log(descriptor.name)  // "fn"
console.log(descriptor.isArrowFunction)  // true
console.log(descriptor.parameters[0].name)  // "a"
console.log(descriptor.parameters[1].name)  // "b"
console.log(descriptor.parameters[1].hasDefault)  // true

Installation

Within a Node.js project, install through npm.

npm install function-descriptor

Or, to use on the web, add:

<!-- Dependency: 'acorn-loose' library. --> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/acorn-loose/8.4.0/acorn-loose.min.js" integrity="sha512-Ffb86Jr5RrEScAcI3I/LQ8Tr/VM6C/I79Icryx8X4cpiBIvdPNKqf2MbXdlpFj61+gCPTWWQkwOw9m/7CHed3A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<!-- This library. -->
<script type="application/javascript" src="https://unpkg.com/function-descriptor/dist/main.umd.js"></script>

Or, poke around the dist folder.

Features

Information you can glean from a function using this library:

  • Minimum number of arguments (minArgs)
  • Maximum number of arguments (maxArgs)
  • Whether it is:
    • An arrow function (isArrowFunction)
    • An async function (isAsyncFunction)
    • A generator function (isGenerator)
    • A class (isClass). In this case, the returned FunctionDescriptor will describe the constructor.
  • Parameter traits (parameters)
    • Parameter name (name)
    • Whether there is a default value (hasDefault)
    • If parameters/arguments are not a 1-1 mapping (destructureType). For example:
      • ...args Spread syntax (destructureType = "spread")
      • { arg1, arg2 } Object destructuring (destructureType = "object")
      • [elem1, elem2] Array destructuring (destructureType = "array")

This library supports a number of features including:

  • Anonymous functions
  • Spread syntax
  • Object and array destructuring
  • Detecting if a parameter has a default value set
  • Class / instance methods
  • Arbitrary strings and comments within the signature, such as the following:
function(myarg="Let's try to break the parser: , myotherarg,\",)\n\n[...args]function(){}{") {
  // Definition
}

Complex Example

Here's an arbitrary Javascript function one might write, using some extended JS concepts when defining the parameter definitions.

function f(x, y=1, { operation, printArgs }, ...args) {
  if (printArgs) {
    console.log("Given extra arguments: " + args)
  }
  if (operation == "add") {
    return x + y
  } else {
    return x - y
  }
}
console.log(describeFunction(f))
FunctionDescriptor {
  f: [Function: f],
  name: 'f',
  isAsync: false,
  isClass: false,
  isGenerator: false,
  parameters: [
    ParamDescriptor {
      name: 'x',
      rawName: 'x',
      destructureType: null,
      hasDefault: false
    },
    ParamDescriptor {
      name: 'y',
      rawName: ...,
      destructureType: null,
      hasDefault: true
    },
    ParamDescriptor {
      name: undefined,
      rawName: ...,
      destructureType: 'object',
      hasDefault: false
    },
    ParamDescriptor {
      name: 'args',
      rawName: ...,
      destructureType: 'spread',
      hasDefault: false
    }
  ],
  minArgs: 1,
  maxArgs: Infinity,
  functionString: ...,
  isArrowFunction: false
}

Some things to note:

  • Arguments formed by object or array destructuring do not have names.
  • When a parameter uses spread syntax, the maximum number of arguments becomes Infinity.
  • When there are defaults, the actual values are not detected (it would be possible to parse these values if they were literals, but that's not implemented).