JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 90
  • Score
    100M100P100Q75969F
  • License VOL

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`.

Package Exports

  • optional-args

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (optional-args) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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.

Fun fact: optional-args calls itself to make its argCount parameter optional. Code is neat.

install

npm install optional-args

example

const optionalArgs = require('optional-args')

const myFn = optionalArgs((foo, options, cb) => {})
myFn(123, function() {}) //  foo -> 123, options -> undefined, cb -> function
myFn(function() {}) // foo -> undefined, options -> undefined, cb -> function

// with default parameters, specify argument count
const myFn = optionalArgs(3, (foo, options = {}, cb) => {})

API

optionalArg([argCount], fn)

  • argCount: number, fn.length the total number of arguments fn accepts. Required for functions that use default parameters since fn.length only counts up to the first default parameter.
  • fn: function the function to be wrapped so that it has optional arguments
  • returns: function