1 | const Promise = require('bluebird');
|
2 | const _ = require('lodash');
|
3 |
|
4 | const asyncMapDeep = (array, key, mapFn, ignoreBranches) =>
|
5 | Promise.map(
|
6 | array,
|
7 | value =>
|
8 | _.isObject(value) || _.isArray(value)
|
9 | ? asyncMapValuesDeep(value, mapFn, ignoreBranches)
|
10 | : mapFn(value, key),
|
11 | { concurrency: 100 },
|
12 | );
|
13 |
|
14 | const asyncMapValuesDeep = (object, mapFn, ignoreBranches) =>
|
15 | Promise.props(
|
16 | _.mapValues(object, (value, key) => {
|
17 | if (_.includes(ignoreBranches, key)) {
|
18 | return value;
|
19 | }
|
20 | return _.isArray(value)
|
21 | ? asyncMapDeep(value, key, mapFn, ignoreBranches)
|
22 | : _.isObject(value)
|
23 | ? asyncMapValuesDeep(value, mapFn, ignoreBranches)
|
24 | : mapFn(value, key);
|
25 | }),
|
26 | );
|
27 |
|
28 | module.exports = {
|
29 | asyncMapValuesDeep,
|
30 | };
|