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

mr-params

v0.3.7

Published

Get parameter names from a given function reference, optionally caching the results.

Downloads

24

Readme

TL;DR

Get parameter names from a given function reference, and optionally cache the results.

Supports

ES6+

  • Typical parameters
  • Array destructuring
  • Object destructuring
  • Comments
  • Default args

Features

  • Cache/Memoization
  • Wrapping results with argument values
  • Parameters are ordered by their appearance in the function definition

Example

It's recommended that you run the factory only once, because it initiates a cache - so initialise it in it's own module and export it:

const factory = require("mr-params").default;
const getParams = factory();
exports = getParams;
const getParams = require("./my-module");
getParams((a) => undefined); // => ["a"]

Options

const factory = require("mr-params").default;
const getParams = factory({
  cache: true, // default.
  debug: false, // default. Used internally. Not useful, for now.
  cacheFactory: nativeCacheFactory // default. Optionally replace the caching mechanism
});

Wrap

You can wrap the results with their related values - typically you'd use an args array:

getParams((a, b, c) => undefined, [1, 2, 3]); // => {a: 1, b: 2, c: 3}

// with: args = ["foo", "bar", "baz"]
getParams((a, b, c) => undefined, args); // don't spread args. => {a: "foo", b: "bar", c: "baz"}

Dependencies

  • @babel/parse

  • @babel/traverse

  • farmhash

In Depth

How It Works

On first usage for a function reference, it will build a Babel AST for the entire function. All tokens are ignored, except parameters that are in scope (of the function body). The results are cached, avoiding the expensive process of rebuilding the AST.

  1. toString() the function and hash it
  2. Check the cache, using the hash as the key, return result on hit, continue on miss
  3. Build an AST for the entire function, and extract the parameters
  4. Store the result in the cache
  5. Return the result

Cache

You can replace the cache with your own implementation:

const getParams = factory({ cacheFactory: myCacheFactory });

The cache factory must return a cache object with the following (TS) interface:

interface ICacheOps {
  get: (key: string) => string[] | false | undefined; // undefined on cache miss.
  put: (key: string, val: string[] | false) => void; // an array of strings, or false
}

This interface is also exported from the main module, if you use Typescript.

Example

const myCacheFactory = (): ICacheOps => {{
  get: (k) => ["foo", "bar", "baz"], // or false
  put: (k, v) => undefined
}}

Explanation

The internal parse() function can return either an array of strings (parameter names), or false (no parameters found). This parse() function feeds the cache, and these are the only two states that are ever put().

On a cache miss, get() must return undefined. On hit, an array of parameter names (strings), or false - for 0 parameter names.

Samples That Pass Tests

getParams((a, b, c) => undefined); // ["a", "b", "c"]

function fn1(a = (x, y = () => undefined, z) => undefined, [ b, [ d, [ f, [ g ] ] ] ], { c: { e: { h: { i } } } }, v) {};
getParams(fn1); // => ["a", "b", "d", "f", "g", "i", "v"]

function fn2(a = (x, y = () => undefined, z) => undefined, [ b, [ d, [ f = 2, [ g = 1 ] = [] ] = [] ] ], { c: { e: { h: { i = 3 } = {} } } }, v) {};
getParams(fn2)l // => ["a", "b", "d", "f", "g", "i", "v"]