Merge pull request #15802 from strapi/feature/async-reduce-utils

feat(utils): add ReduceAsync function
This commit is contained in:
Nathan Pichon 2023-02-15 09:45:39 +01:00 committed by GitHub
commit e51273e285
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 4 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,58 @@ describe('Async utils', () => {
expect(maxOperations).toEqual(2);
});
});
describe('reduceAsync', () => {
test('Should return an 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 without initial value', async () => {
const numberPromiseArray = [Promise.resolve(1), Promise.resolve(2)];
const reduceFunc = reduceAsync(numberPromiseArray);
const result = await reduceFunc(
(previousValue, currentValue) => (previousValue || 10) + currentValue
);
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 with proper message when the provided callback throws 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 with proper message when the input array contains a rejected Promise', async () => {
const numberPromiseArray = [Promise.reject(new Error('input')), Promise.resolve(2)];
const reduceFunc = reduceAsync(numberPromiseArray);
await expect(async () => {
await reduceFunc(() => true, null);
}).rejects.toThrow('input');
});
});
});

View File

@ -1,4 +1,6 @@
export type MapAsync<T = any, R = any> = lodash.CurriedFunction3<
import { CurriedFunction3 } from 'lodash';
export type MapAsync<T = any, R = any> = CurriedFunction3<
T[],
(element: T, index: number) => R | Promise<R>,
{ concurrency?: number },
@ -10,3 +12,10 @@ export type ForEachAsync<T = any, R = any> = (
func: (element: T, index: number) => R | Promise<R>,
options?: { concurrency?: number }
) => Promise<R[]>;
export type ReduceAsync<T = any, V = T, R = V> = CurriedFunction3<
T[],
(accumulator: V | R, current: Awaited<T>, index: number) => R | Promise<R>,
V,
Promise<R>
>;

View File

@ -1,7 +1,7 @@
'use strict';
const pMap = require('p-map');
const { curry } = require('lodash/fp');
const { curry, curryN } = require('lodash/fp');
function pipeAsync(...methods) {
return async (data) => {
@ -20,6 +20,17 @@ function pipeAsync(...methods) {
*/
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 }
*/
@ -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,