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

parameter.js

v1.0.0

Published

Retrieve function parameter name and default values

Downloads

462

Readme

parameter.js

Parameter.js is a simple library that allows you to extract parameter names and default values from any javascript function.

While extracting parameter names used to be an easy task, the introduction of default parameter values, and arrow functions in ES6 complicated the process.

This library was thus created to make extracting parameter names and default values from ES6 functions simple. The library was inspired by the questions and answers posted on this Stack Overflow thread on extracting parameter names from js functions.

Parameter.js

  • Handles functions with comments in parameter body (regardless of content)
  • Handles functions with function calls as default parameters
  • Handles functions with arrow functions as default parameters
  • Handles functions with valid javascript expressions as default parameters

Installation

$ npm install parameter.js

Usage

//es5
var { getParamNames, getParamDefaultValues } = require("parameter.js");
//es6
import { getParamNames, getParamDefaultValues } from 'parameter.js';

Documentation

getParamNames

/**
 * Returns the parameters of the given function as a list of strings
 * @param {any JS function} func 
 */
function getParamNames(func) {
  //Truncated
}

Examples

  • Handles functions with no parameters
function testFunc1() {
  return null;
}
getParamNames(testFunc1) // returns []
  • Handles functions with no default parameters
function testFunc2(param1, param2, param3) {
  return null;
}
getParamNames(testFunc2) // returns ["param1", "param2", "param3"]
  • Handles functions bound to a variable
let testFunc3 = function(param1, param2) {
  return null;
};
getParamNames(testFunc2) // returns ["param1", "param2"]
  • Handles functions with simple default parameters
function testFunc4(param1 = 1, param2 = null, param3 = "Hello World") {
  return null;
}
getParamNames(testFunc4) // returns ["param1", "param2", "param3"]
  • Handles functions with comments in parameter body
function testFunc5(
  param1 = 0.54 /*This is a float*/,
  param2 /* Random close parenthesis )*/,
  /* Random open parenthesis ( */ param3 = "Str"
) {
  return null;
}
getParamNames(testFunc5) // returns ["param1", "param2", "param3"]
  • Handles functions with function calls, expressions, and arrow functions as default parameters.
function testFunc6(
  param1 = Math.round(0.6),
  param2 = (4 + 3) / 2,
  param3 = num => {
    let k = num;
    return k + 2;
  }
) {
  return null;
}
getParamNames(testFunc6) // returns ["param1", "param2", "param3"]

getParamDefaultValues

/**
 * Returns the default parameters of a given function as a list of strings
 * @param {any JS function} func 
 */
function getParamDefaultValues(func) {
  //Truncated
}

Examples

  • Handles functions with no parameters
function testFunc1() {
  return null;
}
getParamDefaultValues(testFunc1) // returns []
  • Handles functions with no default parameters
function testFunc2(param1, param2, param3) {
  return null;
}
getParamDefaultValues(testFunc2) // returns [undefined, undefined, undefined]
  • Handles functions bound to a variable
let testFunc3 = function(param1, param2) {
  return null;
};
getParamNames(testFunc2) // returns [undefined, undefined]
  • Handles functions with simple default parameters
function testFunc4(param1 = 1, param2 = null, param3 = "Hello World") {
  return null;
}
getParamNames(testFunc4) // returns ["1", "null", '"Hello World"']
  • Handles functions with comments in parameter body
function testFunc5(
  param1 = 0.54 /*This is a float*/,
  param2 /* Random close parenthesis )*/,
  /* Random open parenthesis ( */ param3 = "Str"
) {
  return null;
}
getParamNames(testFunc5) // returns ["0.54", undefined, '"Str"']
  • Handles functions with function calls, expressions, and arrow functions as default parameters.
function testFunc6(
  param1 = Math.round(0.6),
  param2 = (4 + 3) / 2,
  param3 = num => {
    let k = num;
    return k + 2;
  }
) {
  return null;
}
getParamNames(testFunc6) // returns ["Math.round(0.6)", "(4 + 3) / 2", "num => {\n    let k = num;\n    return k + 2;\n  }"]