1 | const { passOrGet } = require('./function');
|
2 | const expose = require('./expose');
|
3 |
|
4 | /**
|
5 | * Iterate a `Map` of key/value functions until the first *key*
|
6 | * that produces a truthy result when called and return the outcome
|
7 | * of calling the associated *value*.
|
8 | *
|
9 | * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
|
10 | *
|
11 | * @param {Object} options
|
12 | * @param {Map} options.map
|
13 | * @param {Array} [options.keyArguments=[]]
|
14 | * @param {Array} [options.valueArguments=[]]
|
15 | * @param {*} [options.fallback]
|
16 | * @return {*}
|
17 | */
|
18 | function reduceMap({ map, keyArguments = [], valueArguments = [], fallback }) {
|
19 | for (const [mapKey, mapValue] of map) {
|
20 | if (mapKey(...keyArguments)) {
|
21 | return mapValue(...valueArguments);
|
22 | }
|
23 | }
|
24 |
|
25 | return passOrGet(fallback, valueArguments);
|
26 | }
|
27 |
|
28 | module.exports = expose({
|
29 | reduceMap,
|
30 | });
|