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

reflect-args

v2.0.0

Published

Lets you retrieve argument names, including default values from outside a function.

Downloads

10

Readme

reflect-args

This npm module defines a means of retrieving parameters from a function from outside the function. It works with any function or closure, including methods, but not with constructors wrapped inside ES6 classes.

Installation

$ npm install reflect-args

Usage

Before using it, we need to define it:

const getArgs = require('reflect-args').getArgs

The getArgs function takes in a function and returns a Map containing all the parameters of that function.

Function or Closure

let func = function (foo, bar = 12, test = '12', cb = () => {}) {
	// TODO: body stub
}

console.log(getArgs(func))

/* Expected output:
 * Map {
 *     'foo' => undefined, 
 *     'bar' => 12, 
 *     'test' => '12',
 *     'cb' => [Function: cb]
 * }
 */

Method (Can Be Static)

let Class = class Test
{
	constructor () {}
	
	func (foo, bar) {
		
	}
}
let inst = new Class()

console.log(getArgs(inst.func))

/* Expected output:
 * Map {
 *     'foo' => undefined, 
 *     'bar' => undefined	
 * }
 */

Pattern Matched Parameters

Sometimes you may want to let the end user (programmer) use pattern matching inside their function whose arguments are reflected. During such cases, the getArgs function will give the keys the names of an incrementing range:

let patternMatched = function ({foo, bar}, test, [more, and])
{
	
}

console.log(getArgs(patternMatched))

/* Expected output:
 * Map {
 *     '__0' => '{foo, bar}', 
 *     'test' => undefined, 
 *     '__1' => '[more, and]'
 * }
 */

Discussion

The way this works is by utilizing the Function.prototype.toString function, by extracing the arguments from that string. This means that if the code is obfuscated, this will not work.

That same sentence holds true for reflection done in any other language. If the code is obfuscated, the reflected variable names will be changed into whatsoever they were obfuscated into.

As for most cases, there is no point in minifying (thus obfuscating) server-side code, as we do not actually have to send the data to some client, thus saving on band-width. If, for some reason, you would like to minify server-sided code using this module, make sure the minifier does not obfuscate away variable names.