feat(utils): add reduceAsync

This commit is contained in:
nathan-pichon 2023-01-31 15:14:26 +01:00
parent 9818b82377
commit 5b0e7f6903
No known key found for this signature in database
5 changed files with 70 additions and 5 deletions

View File

@ -16,6 +16,7 @@ Available functions:
- pipeAsync
- mapAsync
- reduceAsync
[See API reference](../../api/Utils) (TODO)

View File

@ -1,6 +1,6 @@
'use strict';
const { pipeAsync, mapAsync } = require('../async');
const { pipeAsync, mapAsync, reduceAsync } = require('../async');
describe('Async utils', () => {
describe('pipeAsync', () => {
@ -77,4 +77,48 @@ describe('Async utils', () => {
expect(maxOperations).toEqual(2);
});
});
describe('reduceAsync', () => {
test('Should return a incremented number', async () => {
const numberPromiseArray = [Promise.resolve(1), Promise.resolve(2)];
const reduceFunc = reduceAsync(numberPromiseArray);
const result = await reduceFunc(
(previousValue, currentValue) => previousValue + currentValue,
10
);
expect(result).toEqual(13);
});
test('Should work with mix of promises and values', async () => {
const numberMixArray = [1, Promise.resolve(2)];
const reduceFunc = reduceAsync(numberMixArray);
const result = await reduceFunc(
(previousValue, currentValue) => previousValue + currentValue,
10
);
expect(result).toEqual(13);
});
test('Should throw an error', async () => {
const numberPromiseArray = [Promise.resolve(1), Promise.resolve(2)];
const reduceFunc = reduceAsync(numberPromiseArray);
await expect(async () => {
await reduceFunc(() => {
throw new Error('test');
});
}).rejects.toThrow('test');
});
test('Should throw an error 2', async () => {
const numberPromiseArray = [Promise.reject(new Error('input')), Promise.resolve(2)];
const reduceFunc = reduceAsync(numberPromiseArray);
await expect(async () => {
await reduceFunc(() => true);
}).rejects.toThrow('input');
});
});
});

View File

@ -6,7 +6,14 @@ export type MapAsync<T = any, R = any> = lodash.CurriedFunction3<
>;
export type ForEachAsync<T = any, R = any> = (
array: T[],
func: (element: T, index: number) => R | Promise<R>,
options?: { concurrency?: number }
array: T[],
func: (element: T, index: number) => R | Promise<R>,
options?: { concurrency?: number }
) => Promise<R[]>;
export type ReduceAsync<T = any, V = T, R = V> = lodash.CurriedFunction3<
T[],
(accumulator: V | R, current: Awaited<T>, index: number) => R | Promise<R>,
V,
Promise<R>
>;

View File

@ -20,6 +20,17 @@ function pipeAsync(...methods) {
*/
const mapAsync = curry(pMap);
/**
* @type { import('./async').ReduceAsync }
*/
const reduceAsync = curry(async (mixedArray, iteratee, initialValue) => {
let acc = initialValue;
for (let i = 0; i < mixedArray.length; i += 1) {
acc = await iteratee(acc, mixedArray[i], i);
}
return acc;
});
/**
* @type { import('./async').ForEachAsync }
*/
@ -29,6 +40,7 @@ const forEachAsync = curry(async (array, func, options) => {
module.exports = {
mapAsync,
reduceAsync,
forEachAsync,
pipeAsync,
};

View File

@ -37,7 +37,7 @@ const providerFactory = require('./provider-factory');
const pagination = require('./pagination');
const sanitize = require('./sanitize');
const traverseEntity = require('./traverse-entity');
const { pipeAsync, mapAsync, forEachAsync } = require('./async');
const { pipeAsync, mapAsync, reduceAsync, forEachAsync } = require('./async');
const convertQueryParams = require('./convert-query-params');
const importDefault = require('./import-default');
const template = require('./template');
@ -83,6 +83,7 @@ module.exports = {
pagination,
pipeAsync,
mapAsync,
reduceAsync,
forEachAsync,
errors,
validateYupSchema,