From 8d99e9e1c44bc2830d27ef8775af02b41dbf4e9d Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Tue, 7 Jan 2025 12:30:05 -0500 Subject: [PATCH 1/2] fix(parallel): clamp error with empty array --- src/async/parallel.ts | 4 ++++ tests/async/parallel.test.ts | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/async/parallel.ts b/src/async/parallel.ts index 42b42e7b..05b0c4dd 100644 --- a/src/async/parallel.ts +++ b/src/async/parallel.ts @@ -66,6 +66,10 @@ export async function parallel( array: readonly T[], func: (item: T) => Promise, ): Promise { + if (array.length === 0) { + return [] + } + const work = array.map((item, index) => ({ index, item, diff --git a/tests/async/parallel.test.ts b/tests/async/parallel.test.ts index a84ad4bb..df95d2df 100644 --- a/tests/async/parallel.test.ts +++ b/tests/async/parallel.test.ts @@ -134,4 +134,11 @@ describe('parallel', () => { test('limit defaults to array length if Infinity is passed', async () => { await testConcurrency(Number.POSITIVE_INFINITY, _.list(1, 3), [1, 2, 3]) }) + + test('returns empty array for empty input', async () => { + const result = await _.parallel(1, [], async () => { + throw new Error('Should not be called') + }) + expect(result).toEqual([]) + }) }) From 9a18baa2d98d6a92894ec6f469a1eb428db654c1 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Tue, 7 Jan 2025 12:46:11 -0500 Subject: [PATCH 2/2] shorten it --- src/async/parallel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/async/parallel.ts b/src/async/parallel.ts index 05b0c4dd..2587cf11 100644 --- a/src/async/parallel.ts +++ b/src/async/parallel.ts @@ -66,7 +66,7 @@ export async function parallel( array: readonly T[], func: (item: T) => Promise, ): Promise { - if (array.length === 0) { + if (!array.length) { return [] }