UNPKG

1.94 kBMarkdownView Raw
1# arity-of
2
3[![Build Status](https://travis-ci.com/mikesamuel/arity-of.svg?branch=master)](https://travis-ci.com/mikesamuel/arity-of)
4[![Dependencies Status](https://david-dm.org/mikesamuel/arity-of/status.svg)](https://david-dm.org/mikesamuel/arity-of)
5[![npm](https://img.shields.io/npm/v/arity-of.svg)](https://www.npmjs.com/package/arity-of)
6[![Coverage Status](https://coveralls.io/repos/github/mikesamuel/arity-of/badge.svg?branch=master)](https://coveralls.io/github/mikesamuel/arity-of?branch=master)
7[![Install Size](https://packagephobia.now.sh/badge?p=arity-of)](https://packagephobia.now.sh/result?p=arity-of)
8[![Known Vulnerabilities](https://snyk.io/test/github/mikesamuel/arity-of/badge.svg?targetFile=package.json)](https://snyk.io/test/github/mikesamuel/arity-of?targetFile=package.json)
9
10An NPM library that exposes max arity and other metadata for JS functions
11
12This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES6-supported environment and complies with the [spec](http://www.ecma-international.org/ecma-262/9.0/) (TODO this is a lie. Write a proposal).
13
14## Concepts
15
16"Arity" refers to the number of parameters a function declares.
17
18`function (a, b, c) {}` declares 3 parameters so has a minimum and maximum arity of 3.
19
20`function (a, b = 0, ...rest) {}` declares 3 parameters. It has a minimum arity of 1 since
21there is no default value for a. It has a maximum arity of 3 since that is the number declared.
22It uses a rest parameter `...rest` so it can actually take additional parameters.
23
24## Installation
25
26```sh
27npm install arity-of
28```
29
30## Usage
31
32```js
33const arityOf = require('arity-of');
34
35function myFunction(a, b = 0, [c, d], ...e) {
36 // ...
37}
38
39myFunction.length; // Minimum arity
40
41const {
42 max, // Maximum arity (including any ...restParamater)
43 usesRest, // True if there is a ...restParameter.
44} = arityOf(myFunction);
45```