Alexandre BODIN 942e646d28
Perf optimizations (#16117)
Co-authored-by: Marc-Roig <marc12info@gmail.com>
2023-03-21 18:37:21 +01:00

46 lines
890 B
JavaScript

'use strict';
const pMap = require('p-map');
const { curry, curryN } = require('lodash/fp');
function pipeAsync(...methods) {
return async (data) => {
let res = data;
for (let i = 0; i < methods.length; i += 1) {
res = await methods[i](res);
}
return res;
};
}
/**
* @type { import('./async').MapAsync }
*/
const mapAsync = curry(pMap);
/**
* @type { import('./async').ReduceAsync }
*/
const reduceAsync = curryN(2, async (mixedArray, iteratee, initialValue) => {
let acc = initialValue;
for (let i = 0; i < mixedArray.length; i += 1) {
acc = await iteratee(acc, await mixedArray[i], i);
}
return acc;
});
/**
* @type { import('./async').ForEachAsync }
*/
const forEachAsync = curry(async (array, func, options) => {
await mapAsync(array, func, options);
});
module.exports = {
mapAsync,
reduceAsync,
forEachAsync,
pipeAsync,
};