# optional-args

> Wraps a function whose last argument is preceded by optional arguments so that when fewer arguments are passed in, missing optional arguments are filled in as `undefined`.

Latest version **2.2.1** (published 2017-03-19) · CC0-1.0 license · 0 weekly downloads

## Install

```sh
npm install optional-args
pnpm add optional-args
yarn add optional-args
bun add optional-args
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.1 |
| Published | 2017-03-19 |
| First published | 2016-11-08 |
| Weekly downloads | 0 |
| License | CC0-1.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Johnny Hauser |
| Maintainers | m59 |
| Keywords | optional, arguments, args, function, simple, last, node, style, callback, browser, browserify |

## Links

- npm: https://www.npmjs.com/package/optional-args
- Repository: https://github.com/m59peacemaker/js-optional-args
- Homepage: https://github.com/m59peacemaker/js-optional-args#readme
- Issues: https://github.com/m59peacemaker/js-optional-args/issues
- npm.io page: https://npm.io/package/optional-args

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.2.1 (latest) — 2017-03-19
- 2.2.0 — 2017-03-19
- 2.1.2 — 2017-03-19
- 2.1.1 — 2017-02-09
- 2.1.0 — 2016-11-08
- 2.0.0 — 2016-11-08
- 1.1.0 — 2016-11-08

## README

# optional-args

Wraps a function whose last argument is preceded by optional arguments so that when fewer arguments are passed in, missing optional arguments are filled in as `undefined`.

The is useful for functions that have an API such as `(input, options, cb)`, but the function allows `options` to be omitted so that it can be called as `(input, cb)`. Rather than having code that examines the arguments to make some optional, just wrap the function with `optionalArgs()` and it will just work.

## install

```sh
$ npm install optional-args
```

## example

```js
const optionalArgs = require('optional-args')

// 1 optional argument, 3 total arguments
const myFn = optionalArgs(1, 3, (input, options, cb) => {})

myFn(123, () => {})
// input === 123
// options === undefined
// typeof cb === 'function'

myFn(123)
// input === 123
// options === undefined
// cb === undefined
```

## API

### `optionalArgs(optionalArgCount, argCount, fn)`

- `optionalArgCount: number` the number of arguments that are optional
- `argCount: number` the total number of arguments `fn` accepts
- `fn: function` the function to be wrapped so that it has optional arguments
- **returns**: `function` the wrapped function

---
_Source: https://npm.io/package/optional-args · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
