Skip to content

Commit

Permalink
fix: optimize array merge
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed May 20, 2023
1 parent f7d4a75 commit 240ce25
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
11 changes: 1 addition & 10 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/utils/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
})

Expand Down

0 comments on commit 240ce25

Please sign in to comment.