Skip to content

Commit

Permalink
feat: split regular and distinct array merge + support multiple input…
Browse files Browse the repository at this point in the history
… arrays
  • Loading branch information
tada5hi committed May 20, 2023
1 parent 44c8f75 commit f7d4a75
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
isObject,
isSafeKey,
isSafeObject,
mergeArrays,
mergeArrays, mergeArraysDistinct,
} from './utils';

export function baseMerger<A extends Record<string, any>, B extends Record<string, any>>(
Expand Down Expand Up @@ -68,12 +68,16 @@ export function baseMerger<A extends Record<string, any>, B extends Record<strin
switch (options.priority) {
case PriorityName.LEFT:
Object.assign(target, {
[key]: mergeArrays(target[key], source[key], options.arrayDistinct),
[key]: options.arrayDistinct ?
mergeArraysDistinct(target[key], source[key]) :
mergeArrays(target[key], source[key]),
});
break;
case PriorityName.RIGHT:
Object.assign(target, {
[key]: mergeArrays(source[key], target[key], options.arrayDistinct),
[key]: options.arrayDistinct ?
mergeArraysDistinct(source[key], target[key]) :
mergeArrays(source[key], target[key]),
});
break;
}
Expand Down
21 changes: 12 additions & 9 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { isEqual } from './check';

export function distinctArray(arr: any[]) {
export function distinctArray(arr: any[]) : any[] {
const copy = [...arr];

for (let i = 0; i < copy.length; i++) {
Expand All @@ -21,16 +21,19 @@ export function distinctArray(arr: any[]) {
return copy;
}

export function mergeArrays(
first: any[],
second: any[],
arrayDistinct: boolean,
) {
const merged = first.concat(second);
export function mergeArrays(...sources: any[][]) : any[] {
let merged = sources.shift();
if (!merged) {
return [];
}

if (arrayDistinct) {
return distinctArray(merged);
for (let i = 0; i < sources.length; i++) {
merged = merged.concat(sources[i]);
}

return merged;
}

export function mergeArraysDistinct(...sources: any[][]) : any[] {
return distinctArray(mergeArrays(...sources));
}
12 changes: 10 additions & 2 deletions test/unit/utils/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* view the LICENSE file that was distributed with this source code.
*/

import {distinctArray, mergeArrays} from "../../../src";
import {distinctArray, mergeArrays, mergeArraysDistinct} from "../../../src";

describe('src/utils/array', function () {
it('should distinct array', () => {
Expand Down Expand Up @@ -50,9 +50,17 @@ describe('src/utils/array', function () {
})

it('should merge arrays', () => {
expect(mergeArrays()).toEqual([]);

expect(mergeArrays(['foo'], ['bar'])).toEqual(['foo', 'bar']);

expect(mergeArrays(['foo'], ['bar'], ['baz'])).toEqual(['foo', 'bar', 'baz']);
})

it('should merge arrays without duplicates', () => {
let x = ['foo', 'bar', 'baz'];
let y = ['baz', 'moo', 'bar', 'mal'];

expect(mergeArrays(x, y, true)).toEqual(['foo', 'bar', 'baz', 'moo', 'mal']);
expect(mergeArraysDistinct(x, y)).toEqual(['foo', 'bar', 'baz', 'moo', 'mal']);
})
});

0 comments on commit f7d4a75

Please sign in to comment.