arity-of
v1.0.2
Published
Exposes max arity and other metadata for JS functions
Downloads
10
Readme
arity-of
An NPM library that exposes max arity and other metadata for JS functions
This package implements the es-shim API interface. It works in an ES6-supported environment and complies with the spec (TODO this is a lie. Write a proposal).
Concepts
"Arity" refers to the number of parameters a function declares.
function (a, b, c) {}
declares 3 parameters so has a minimum and maximum arity of 3.
function (a, b = 0, ...rest) {}
declares 3 parameters. It has a minimum arity of 1 since
there is no default value for a. It has a maximum arity of 3 since that is the number declared.
It uses a rest parameter ...rest
so it can actually take additional parameters.
Installation
npm install arity-of
Usage
const arityOf = require('arity-of');
function myFunction(a, b = 0, [c, d], ...e) {
// ...
}
myFunction.length; // Minimum arity
const {
max, // Maximum arity (including any ...restParamater)
usesRest, // True if there is a ...restParameter.
} = arityOf(myFunction);