README.mdView |
---|
| 1 … | +# map-args |
| 2 … | + |
| 3 … | +Functions that take flexible arguments are fun to use but not fun to read or write: |
| 4 … | +```js |
| 5 … | +function (keys, opts, cb) { |
| 6 … | + if (typeof keys == 'function') { |
| 7 … | + cb = keys |
| 8 … | + keys = null |
| 9 … | + opts = null |
| 10 … | + } |
| 11 … | + else if (typeof opts == 'function') { |
| 12 … | + cb = opts |
| 13 … | + opts = keys |
| 14 … | + keys = null |
| 15 … | + } |
| 16 … | + |
| 17 … | + // etc |
| 18 … | +} |
| 19 … | +``` |
| 20 … | + |
| 21 … | +In case it's not immediately obvious, you can use this function like : |
| 22 … | + |
| 23 … | +``` |
| 24 … | +func(keys, opts, cb) |
| 25 … | +func(opts, cb) |
| 26 … | +func(cb) |
| 27 … | +``` |
| 28 … | + |
| 29 … | +## Usage |
| 30 … | + |
| 31 … | +```js |
| 32 … | +const mapArgs = require('map-args') |
| 33 … | + |
| 34 … | +function (keys, opts, cb) { |
| 35 … | + mapArgs( |
| 36 … | + ['object function', [opts, cb]], |
| 37 … | + ['function', [cb] ] |
| 38 … | + ) |
| 39 … | + |
| 40 … | + // get on with your day |
| 41 … | +} |
| 42 … | +``` |
| 43 … | + |
| 44 … | +## API |
| 45 … | + |
| 46 … | +```js |
| 47 … | +const mapArgs = require('map-args') |
| 48 … | +``` |
| 49 … | + |
| 50 … | +`mapArgs([match1, match2, ...])` |
| 51 … | + |
| 52 … | +Each match is an as array with two elements: `[inputMatcher, outputMap]` |
| 53 … | + |
| 54 … | +The `inputMatcher` can be: |
| 55 … | +- a String of `n` space-seperate types e.g. `'string object function'` |
| 56 … | +- an Array of `n` type matching functions or strings e.g `[isFeed, 'object', 'function']` |
| 57 … | + |
| 58 … | +The `outputMap` is an array of the `n` arguments you're mapping to. |
| 59 … | + |
| 60 … | +Map-args goes through the matchers in order, and if it finds a match it maps the arguments accordingly. |
| 61 … | +If no match is found the arguments will be left as as they were. |
| 62 … | + |
| 63 … | +Matches which map less than the total number of possible args will set non-mapped args to `null`. |
| 64 … | + |
| 65 … | + |
| 66 … | +## Install |
| 67 … | + |
| 68 … | +With [npm](https://npmjs.org/) installed, run |
| 69 … | + |
| 70 … | +``` |
| 71 … | +$ npm install map-args |
| 72 … | +``` |
| 73 … | + |
| 74 … | + |
| 75 … | +## See Also |
| 76 … | + |
| 77 … | +- [`noffle/common-readme`](https://github.com/noffle/common-readme) |
| 78 … | +- ... |
| 79 … | + |
| 80 … | +## License |
| 81 … | + |
| 82 … | +AGPL-3.0 |
| 83 … | + |