git ssb

0+

mixmix / map-args



Branch:
mix irving committed wipLatest: b82a5cf on 2/11/2018, 11:31:12 AM
📄.gitignore
📄README.md
📄index.js
📄package-lock.json
📄package.json
📄test.js
README.md

map-args

Functions that take flexible arguments are fun to use but not fun to read or write:

function (keys, opts, cb) {
  if (typeof keys == 'function') {
    cb = keys
    keys = null
    opts = null
  }
  else if (typeof opts == 'function') {
    cb = opts
    opts = keys
    keys = null
  }

  // etc
}

In case it's not immediately obvious, you can use this function like :

func(keys, opts, cb)
func(opts, cb)
func(cb)

Usage

const mapArgs = require('map-args')

function (keys, opts, cb) {
  mapArgs([
    ['object function', [opts, cb]],
    ['function',        [cb]      ]
  ])

  // get on with your day
}

API

const mapArgs = require('map-args')

function (arg1, arg2, ...) {
  mapArgs([match1, match2, ...])
)

Each match is an as array with two elements: [signatureMatcher, outputMap]

The signatureMatcher can be:

The outputMap is an array of the n arguments which represent the mapped signature of your arguments (arg1, arg2, etc).

Map-args goes through the signature matchers in order and executes the map associated with the first match that fits. If no match is found the arguments will be left as as they were.

Matches which map less than the total number of possible args will set non-mapped args to null.

Install

With npm installed, run

$ npm install map-args

See Also

License

AGPL-3.0

Built with git-ssb-web