diff --git a/packages/core/utils/src/__tests__/assign.test.ts b/packages/core/utils/src/__tests__/assign.test.ts index 49b2333656..4c074df5b9 100644 --- a/packages/core/utils/src/__tests__/assign.test.ts +++ b/packages/core/utils/src/__tests__/assign.test.ts @@ -25,6 +25,20 @@ describe('merge strategy', () => { filter: { a: 'a2' }, }); }); + it('case 1-1', () => { + const obj = assign( + { + filter: { a: 'a2' }, + }, + {}, + { + filter: 'andMerge', + }, + ); + expect(obj).toMatchObject({ + filter: { a: 'a2' }, + }); + }); it('case 2', () => { const obj = assign( { @@ -39,6 +53,20 @@ describe('merge strategy', () => { filter: { a: 'a1' }, }); }); + it('case 2-2', () => { + const obj = assign( + {}, + { + filter: { a: 'a1' }, + }, + { + filter: 'andMerge', + }, + ); + expect(obj).toMatchObject({ + filter: { a: 'a1' }, + }); + }); it('case 3', () => { const obj = assign( { @@ -55,6 +83,22 @@ describe('merge strategy', () => { filter: { a: 'a1' }, }); }); + it('case 3-2', () => { + const obj = assign( + { + filter: undefined, + }, + { + filter: { a: 'a1' }, + }, + { + filter: 'andMerge', + }, + ); + expect(obj).toMatchObject({ + filter: { a: 'a1' }, + }); + }); it('case 4', () => { const obj = assign( { @@ -120,6 +164,50 @@ describe('merge strategy', () => { filter: { a: 'a1' }, }); }); + it('case 1', () => { + const obj = assign( + { + filter: { a: 'a2' }, + }, + {}, + { + filter: 'orMerge', + }, + ); + expect(obj).toMatchObject({ + filter: { a: 'a2' }, + }); + }); + it('case 2', () => { + const obj = assign( + {}, + { + filter: { a: 'a1' }, + }, + { + filter: 'orMerge', + }, + ); + expect(obj).toMatchObject({ + filter: { a: 'a1' }, + }); + }); + it('case 3', () => { + const obj = assign( + { + filter: undefined, + }, + { + filter: { a: 'a1' }, + }, + { + filter: 'orMerge', + }, + ); + expect(obj).toMatchObject({ + filter: { a: 'a1' }, + }); + }); it('case 4', () => { const obj = assign( { @@ -171,6 +259,20 @@ describe('merge strategy', () => { key1: ['val1'], }); }); + it('case 2', () => { + const obj = assign( + {}, + { + key1: ['val1'], + }, + { + key1: 'intersect', + }, + ); + expect(obj).toMatchObject({ + key1: ['val1'], + }); + }); it('case 3', () => { const obj = assign( {}, @@ -185,6 +287,20 @@ describe('merge strategy', () => { key1: ['val2'], }); }); + it('case 3', () => { + const obj = assign( + { + key1: ['val2'], + }, + {}, + { + key1: 'intersect', + }, + ); + expect(obj).toMatchObject({ + key1: ['val2'], + }); + }); it('case 4', () => { const obj = assign( { @@ -236,6 +352,20 @@ describe('merge strategy', () => { key1: ['val1', 'val2'], }); }); + it('case 2', () => { + const obj = assign( + {}, + { + key1: ['val1'], + }, + { + key1: 'union', + }, + ); + expect(obj).toMatchObject({ + key1: ['val1'], + }); + }); it('case 2', () => { const obj = assign( { @@ -264,6 +394,20 @@ describe('merge strategy', () => { key1: ['val2'], }); }); + it('case 3', () => { + const obj = assign( + { + key1: ['val2'], + }, + {}, + { + key1: 'union', + }, + ); + expect(obj).toMatchObject({ + key1: ['val2'], + }); + }); it('case 4', () => { const obj = assign( { @@ -364,6 +508,129 @@ describe('merge strategy', () => { }); }); + describe('overwrite', () => { + it('case 1', () => { + const obj = assign( + { + key1: 'a', + }, + { + key1: 'b', + }, + { + key1: 'overwrite', + }, + ); + expect(obj).toMatchObject({ + key1: 'b', + }); + }); + it('case 2', () => { + const obj = assign( + { + key1: 'a', + }, + {}, + { + key1: 'overwrite', + }, + ); + expect(obj).toMatchObject({ + key1: 'a', + }); + }); + it('case 3', () => { + const obj = assign( + {}, + { + key1: 'a', + }, + { + key1: 'overwrite', + }, + ); + expect(obj).toMatchObject({ + key1: 'a', + }); + }); + it('case 4', () => { + const obj = assign( + { + key1: 'a', + }, + { + key1: undefined, + }, + { + key1: 'overwrite', + }, + ); + expect(obj).toMatchObject({ + key1: 'a', + }); + }); + it('case 5', () => { + const obj = assign( + { + key1: 'a', + }, + { + key1: null, + }, + { + key1: 'overwrite', + }, + ); + expect(obj).toMatchObject({ + key1: null, + }); + }); + it('case 6', () => { + const obj = assign( + { + key1: 'a', + }, + { + key1: '', + }, + { + key1: 'overwrite', + }, + ); + expect(obj).toMatchObject({ + key1: '', + }); + }); + it('case 7', () => { + const obj = assign( + { + key1: 'a,b,c', + }, + {}, + { + key1: 'overwrite', + }, + ); + expect(obj).toMatchObject({ + key1: ['a', 'b', 'c'], + }); + }); + it('case 8', () => { + const obj = assign( + {}, + { + key1: 'a,b,c', + }, + { + key1: 'overwrite', + }, + ); + expect(obj).toMatchObject({ + key1: ['a', 'b', 'c'], + }); + }); + }); + describe('default = deepmerge', () => { it('case 1', () => { const obj = assign( @@ -453,6 +720,11 @@ describe('merge strategy', () => { resourceIndex: 'n0jylid5rqa', actionName: 'getJsonSchema', values: {}, + sort: 'id', + fields: ['id'], + except: ['id'], + whitelist: ['id1'], + blacklist: ['id2'], }, {}, { @@ -469,6 +741,11 @@ describe('merge strategy', () => { resourceIndex: 'n0jylid5rqa', actionName: 'getJsonSchema', values: {}, + sort: 'id', + fields: ['id'], + except: ['id'], + whitelist: ['id1'], + blacklist: ['id2'], }); }); }); diff --git a/packages/core/utils/src/assign.ts b/packages/core/utils/src/assign.ts index 44a368a0ff..81ad36b314 100644 --- a/packages/core/utils/src/assign.ts +++ b/packages/core/utils/src/assign.ts @@ -32,8 +32,14 @@ function getKeys(target: any) { export const mergeStrategies = new Map(); -mergeStrategies.set('overwrite', (_, y) => { - if (typeof y === 'string') { +mergeStrategies.set('overwrite', (x, y) => { + if (y === undefined) { + if (typeof x === 'string' && x.includes(',')) { + return x.split(','); + } + return x; + } + if (typeof y === 'string' && y.includes(',')) { y = y.split(','); } return y;