fix: assign overwrite strategy

This commit is contained in:
chenos 2025-04-10 09:06:32 +08:00
parent f9b2720ef2
commit 29d7cf3dbf
2 changed files with 285 additions and 2 deletions

View File

@ -25,6 +25,20 @@ describe('merge strategy', () => {
filter: { a: 'a2' }, filter: { a: 'a2' },
}); });
}); });
it('case 1-1', () => {
const obj = assign(
{
filter: { a: 'a2' },
},
{},
{
filter: 'andMerge',
},
);
expect(obj).toMatchObject({
filter: { a: 'a2' },
});
});
it('case 2', () => { it('case 2', () => {
const obj = assign( const obj = assign(
{ {
@ -39,6 +53,20 @@ describe('merge strategy', () => {
filter: { a: 'a1' }, filter: { a: 'a1' },
}); });
}); });
it('case 2-2', () => {
const obj = assign(
{},
{
filter: { a: 'a1' },
},
{
filter: 'andMerge',
},
);
expect(obj).toMatchObject({
filter: { a: 'a1' },
});
});
it('case 3', () => { it('case 3', () => {
const obj = assign( const obj = assign(
{ {
@ -55,6 +83,22 @@ describe('merge strategy', () => {
filter: { a: 'a1' }, 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', () => { it('case 4', () => {
const obj = assign( const obj = assign(
{ {
@ -120,6 +164,50 @@ describe('merge strategy', () => {
filter: { a: 'a1' }, 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', () => { it('case 4', () => {
const obj = assign( const obj = assign(
{ {
@ -171,6 +259,20 @@ describe('merge strategy', () => {
key1: ['val1'], key1: ['val1'],
}); });
}); });
it('case 2', () => {
const obj = assign(
{},
{
key1: ['val1'],
},
{
key1: 'intersect',
},
);
expect(obj).toMatchObject({
key1: ['val1'],
});
});
it('case 3', () => { it('case 3', () => {
const obj = assign( const obj = assign(
{}, {},
@ -185,6 +287,20 @@ describe('merge strategy', () => {
key1: ['val2'], key1: ['val2'],
}); });
}); });
it('case 3', () => {
const obj = assign(
{
key1: ['val2'],
},
{},
{
key1: 'intersect',
},
);
expect(obj).toMatchObject({
key1: ['val2'],
});
});
it('case 4', () => { it('case 4', () => {
const obj = assign( const obj = assign(
{ {
@ -236,6 +352,20 @@ describe('merge strategy', () => {
key1: ['val1', 'val2'], key1: ['val1', 'val2'],
}); });
}); });
it('case 2', () => {
const obj = assign(
{},
{
key1: ['val1'],
},
{
key1: 'union',
},
);
expect(obj).toMatchObject({
key1: ['val1'],
});
});
it('case 2', () => { it('case 2', () => {
const obj = assign( const obj = assign(
{ {
@ -264,6 +394,20 @@ describe('merge strategy', () => {
key1: ['val2'], key1: ['val2'],
}); });
}); });
it('case 3', () => {
const obj = assign(
{
key1: ['val2'],
},
{},
{
key1: 'union',
},
);
expect(obj).toMatchObject({
key1: ['val2'],
});
});
it('case 4', () => { it('case 4', () => {
const obj = assign( 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', () => { describe('default = deepmerge', () => {
it('case 1', () => { it('case 1', () => {
const obj = assign( const obj = assign(
@ -453,6 +720,11 @@ describe('merge strategy', () => {
resourceIndex: 'n0jylid5rqa', resourceIndex: 'n0jylid5rqa',
actionName: 'getJsonSchema', actionName: 'getJsonSchema',
values: {}, values: {},
sort: 'id',
fields: ['id'],
except: ['id'],
whitelist: ['id1'],
blacklist: ['id2'],
}, },
{}, {},
{ {
@ -469,6 +741,11 @@ describe('merge strategy', () => {
resourceIndex: 'n0jylid5rqa', resourceIndex: 'n0jylid5rqa',
actionName: 'getJsonSchema', actionName: 'getJsonSchema',
values: {}, values: {},
sort: 'id',
fields: ['id'],
except: ['id'],
whitelist: ['id1'],
blacklist: ['id2'],
}); });
}); });
}); });

View File

@ -32,8 +32,14 @@ function getKeys(target: any) {
export const mergeStrategies = new Map<MergeStrategyType, MergeStrategyFunc>(); export const mergeStrategies = new Map<MergeStrategyType, MergeStrategyFunc>();
mergeStrategies.set('overwrite', (_, y) => { mergeStrategies.set('overwrite', (x, y) => {
if (typeof y === 'string') { if (y === undefined) {
if (typeof x === 'string' && x.includes(',')) {
return x.split(',');
}
return x;
}
if (typeof y === 'string' && y.includes(',')) {
y = y.split(','); y = y.split(',');
} }
return y; return y;