1.1.2 • Published 4 years ago

paramnames v1.1.2

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

paramnames

Returns function parameter names using the acorn parser, thus slower than regex implementations. Works with all kinds of functions and parameters. Also works with classes returning the constructor parameter names. Deconstructed parameter names are undefined.

npm version

API

getParameterNames(fn: Function): Array<string | undefined>

Useful information

Returns undefined if the argument is not a function.

Deconstructed parameter names are undefined.

Native function's parameter names are [].

If a class does not have a constructor the function will try to look it up in the prototype. It will go up the inheritance hierarchy until it finds the constructor or hits a native function. If the constructor is still not found, it will return the default constructor's parameter names [].

Examples

const getParameterNames = require("paramnames");

function add(a, b) {
  return a + b;
}

console.log(getParameterNames(add)); // ["a", "b"]
const getParameterNames = require("paramnames");

const fn = (a, [b, c], ...d) => {};

console.log(getParameterNames(fn)); // ["a", undefined, "d"]
const getParameterNames = require("paramnames");

const obj = {
  async *["f" + "n"](a = (1, 2, 3), b = [{}, x => {}], { x, y: { z } }, ...rest) {}
};

console.log(getParameterNames(obj.fn)); // ["a", "b", undefined, "rest"]
const getParameterNames = require("paramnames");

class Sample {
  constructor(a, b) {}
}

console.log(getParameterNames(Sample)); // ["a", "b"]
const getParameterNames = require("paramnames");

class Sample {}

console.log(getParameterNames(Sample)); // []
const getParameterNames = require("paramnames");

class Base {
  constructor(a, b) {}
}

class Child extends Base {}

console.log(getParameterNames(Child)); // ["a", "b"]
const getParameterNames = require("paramnames");

class Base {
  constructor(a, b) {}
}

class Child1 extends Base {}

class Child2 extends Child1 {
  constructor(c, d) {
    super(a, b);
  }
}

class Child3 extends Child2 {}

console.log(getParameterNames(Child3)); // ["c", "d"]
const getParameterNames = require("paramnames");

console.log(getParameterNames(console.log)); // []

console.log(getParameterNames(isNaN)); // []

console.log(getParameterNames(Object)); // []

console.log(getParameterNames(Number)); // []
1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago