From 240ce2577eb99ee12fb9186b2f41fddb610cf3c0 Mon Sep 17 00:00:00 2001 From: tada5hi Date: Sat, 20 May 2023 14:02:40 +0200 Subject: [PATCH] fix: optimize array merge --- src/utils/array.ts | 11 +---------- test/unit/utils/array.spec.ts | 4 ++++ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/utils/array.ts b/src/utils/array.ts index 92207e3..cf1e9cd 100644 --- a/src/utils/array.ts +++ b/src/utils/array.ts @@ -22,16 +22,7 @@ export function distinctArray(arr: any[]) : any[] { } export function mergeArrays(...sources: any[][]) : any[] { - let merged = sources.shift(); - if (!merged) { - return []; - } - - for (let i = 0; i < sources.length; i++) { - merged = merged.concat(sources[i]); - } - - return merged; + return ([] as any[]).concat.apply([], [...sources]); } export function mergeArraysDistinct(...sources: any[][]) : any[] { diff --git a/test/unit/utils/array.spec.ts b/test/unit/utils/array.spec.ts index f6ca506..54d9556 100644 --- a/test/unit/utils/array.spec.ts +++ b/test/unit/utils/array.spec.ts @@ -54,6 +54,10 @@ describe('src/utils/array', function () { expect(mergeArrays(['foo'], ['bar'])).toEqual(['foo', 'bar']); + expect(mergeArrays(['foo', 'bar'], ['baz'])).toEqual(['foo', 'bar', 'baz']); + + expect(mergeArrays(['foo', 'bar'], [['baz']])).toEqual(['foo', 'bar', ['baz']]); + expect(mergeArrays(['foo'], ['bar'], ['baz'])).toEqual(['foo', 'bar', 'baz']); })