Files: a613d7d871a2785faadddb73e2d8117f58855d9e / README.md
1547 bytesRaw
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')
mapArgs([match1, match2, ...])
Each match is an as array with two elements: [inputMatcher, outputMap]
The inputMatcher
can be:
- a String of
n
space-seperate types e.g.'string object function'
- an Array of
n
type matching functions or strings e.g[isFeed, 'object', 'function']
The outputMap
is an array of the n
arguments you're mapping to.
Map-args goes through the matchers in order, and if it finds a match it maps the arguments accordingly. 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