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 |
|
10 | An NPM library that exposes max arity and other metadata for JS functions
|
11 |
|
12 | This 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
|
21 | there is no default value for a. It has a maximum arity of 3 since that is the number declared.
|
22 | It uses a rest parameter `...rest` so it can actually take additional parameters.
|
23 |
|
24 | ## Installation
|
25 |
|
26 | ```sh
|
27 | npm install arity-of
|
28 | ```
|
29 |
|
30 | ## Usage
|
31 |
|
32 | ```js
|
33 | const arityOf = require('arity-of');
|
34 |
|
35 | function myFunction(a, b = 0, [c, d], ...e) {
|
36 | // ...
|
37 | }
|
38 |
|
39 | myFunction.length; // Minimum arity
|
40 |
|
41 | const {
|
42 | max, // Maximum arity (including any ...restParamater)
|
43 | usesRest, // True if there is a ...restParameter.
|
44 | } = arityOf(myFunction);
|
45 | ```
|