Skip to content

Commit a01815e

Browse files
authored
fix: list override (#443)
1 parent cfb8f48 commit a01815e

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/utils/set.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,20 @@ export function merge<T extends object>(...sources: T[]) {
7676
function internalMerge(path: Path) {
7777
const value = get(src, path);
7878

79-
if (isObject(value) || Array.isArray(value)) {
79+
const isArr = Array.isArray(value);
80+
81+
if (isArr || isObject(value)) {
8082
// Only add not loop obj
8183
if (!loopSet.has(value)) {
8284
loopSet.add(value);
8385

84-
// Init container if not exist
8586
const originValue = get(clone, path);
86-
if (!originValue || typeof originValue !== 'object') {
87+
88+
if (isArr) {
89+
// Array will always be override
90+
clone = set(clone, path, []);
91+
} else if (!originValue || typeof originValue !== 'object') {
92+
// Init container if not exist
8793
clone = set(clone, path, createEmpty(value));
8894
}
8995

tests/utils.test.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,34 +107,39 @@ describe('utils', () => {
107107
});
108108
});
109109

110-
it('array', () => {
110+
it('array is replacement', () => {
111111
const merged = merge([], [{ a: 1 }], [{ b: 2 }]);
112112

113113
expect(merged).toEqual([
114114
{
115-
a: 1,
116115
b: 2,
117116
},
118117
]);
119118
});
120119

120+
it('array is replacement - sub field', () => {
121+
const merged = merge({ a: 1, users: [1, 2] }, { users: [] });
122+
123+
expect(merged).toEqual({ a: 1, users: [] });
124+
});
125+
121126
it('not cover', () => {
122127
const merged = merge(
123-
[],
124-
[{ a: { e: 8 }, b: 2 }],
125-
[{ a: { f: 9 }, c: 3 }],
128+
{},
129+
{ _: { a: { e: 8 }, b: 2 } },
130+
{ _: { a: { f: 9 }, c: 3 } },
126131
);
127132

128-
expect(merged).toEqual([
129-
{
133+
expect(merged).toEqual({
134+
_: {
130135
a: {
131136
e: 8,
132137
f: 9,
133138
},
134139
b: 2,
135140
c: 3,
136141
},
137-
]);
142+
});
138143
});
139144

140145
it('DayObject', () => {

0 commit comments

Comments
 (0)