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({
|
19 | map,
|
20 | keyArguments = [],
|
21 | valueArguments = [],
|
22 | fallback,
|
23 | }) {
|
24 | for (const [mapKey, mapValue] of map) {
|
25 | if (mapKey(...keyArguments)) {
|
26 | return mapValue(...valueArguments);
|
27 | }
|
28 | }
|
29 |
|
30 | return passOrGet(fallback, valueArguments);
|
31 | }
|
32 |
|
33 | module.exports = expose({
|
34 | reduceMap,
|
35 | });
|