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

get-param-names

v1.0.1

Published

The best library to parse a function into its parameter names.

Downloads

62

Readme

get-param-names

The best library to parse a function into its parameter names.

Created because of dissatisfaction with existing npm libraries:

Advantages of this library over others:

  • Extremely easy user-side syntax
  • Support for object and Array decomposers passed as function parameters
  • Low package size
  • Intuitive return result
  • Attractive and readable code
  • Runs synchronously and fast
  • Uses the latest ECMAScript 6 specifications

Maintained by theLAZYmd, please report any issues found.

Applications

Parameter grabbing:

Let's say all your data is stored in a big data object (let's call that data message). Let's say you have an event listener that routes you to a particular function depending on what the event is. That function now takes specific pieces of information in order to work, let's say:

function onNewMessage(content, member) {}

where the content and member properties you need to grab are properties of message.

Now what you could do is write a deconstructor into each of your functions, like:

function onNewMessage({content, member}) {}

but that might get annoying if you're not used to it and you might have to change all your previous work and so on. Plus, some people are stylistically opposed to such things

So what you can do instead, is tell node to find the parameters you need from the function, grab those, and send them in the function call, without ever specifically hard-coding them. For instance:

let params = getParamNames(onToxicUsers);
let args = params.map(p => data[p]);
onNewMessage(...args);

and voilà! You've parsed the arguments to the function without ever knowing what they were. This allows you to create a universal system for all your function calls.

Example Usages

Installation:

Use npm install get-param-names to download the package to your workspace. Then write the following at the top of any file that you wish to use the module:

const getParamNames = require('get-param-names');

and simply use the constant as a function wherever needed. Returns an array of parameter names, as specified in the function definition.

Example 1

Simple named function
function f0(self, meme, init) {
	return [self, meme, init];
}
console.log(getParamNames(f0)); // ["self", "meme", "init"]

Example 2

Simple named function with default parameters
function f1(str = 'hello', arr = ['what\'s up']) {
	return [str, arr];
}
console.log(getParamNames(f1)); // ["str", "arr"]

Example 3

Simple arrow function with string parameters
let f2 = (meme, param) => [meme, param];
console.log(getParamNames(f2)); // ["meme", "param"]

Example 4

Function with deconstructed Object parameter
async function f3(ids, {
	fetch = false,
	filter = user => user,
	arr = []
} = {}) {
	return [ids, fetch, filter.toString(), arr];
}
console.log(getParamNames(f3)); // [ 'ids', { fetch: false, filter: 'user => user', arr: [] } ]

Example 5

Complex function with deconstructed Object parameter, deconstructed Array parameter, and default Object and Array values
async function f4({
	item1,
	item2 = {
		prop1: true,
		prop2: false,
		prop3: ['array', 'of', 'strings']
	}
} = {}, [item3, item4, [item5, item6]]) {
	return [item1, item2, item3, item4, item5, item6];
}
console.log(getParamNames(f4)); //[{"item2":{"prop1":true,"prop2":false,"prop3":["array","of","strings"]}},["item3","item4",["item5","item6"]]]