File tree Expand file tree Collapse file tree 2 files changed +22
-11
lines changed Expand file tree Collapse file tree 2 files changed +22
-11
lines changed Original file line number Diff line number Diff line change @@ -76,14 +76,20 @@ export function merge<T extends object>(...sources: T[]) {
76
76
function internalMerge ( path : Path ) {
77
77
const value = get ( src , path ) ;
78
78
79
- if ( isObject ( value ) || Array . isArray ( value ) ) {
79
+ const isArr = Array . isArray ( value ) ;
80
+
81
+ if ( isArr || isObject ( value ) ) {
80
82
// Only add not loop obj
81
83
if ( ! loopSet . has ( value ) ) {
82
84
loopSet . add ( value ) ;
83
85
84
- // Init container if not exist
85
86
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
87
93
clone = set ( clone , path , createEmpty ( value ) ) ;
88
94
}
89
95
Original file line number Diff line number Diff line change @@ -107,34 +107,39 @@ describe('utils', () => {
107
107
} ) ;
108
108
} ) ;
109
109
110
- it ( 'array' , ( ) => {
110
+ it ( 'array is replacement ' , ( ) => {
111
111
const merged = merge ( [ ] , [ { a : 1 } ] , [ { b : 2 } ] ) ;
112
112
113
113
expect ( merged ) . toEqual ( [
114
114
{
115
- a : 1 ,
116
115
b : 2 ,
117
116
} ,
118
117
] ) ;
119
118
} ) ;
120
119
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
+
121
126
it ( 'not cover' , ( ) => {
122
127
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 } } ,
126
131
) ;
127
132
128
- expect ( merged ) . toEqual ( [
129
- {
133
+ expect ( merged ) . toEqual ( {
134
+ _ : {
130
135
a : {
131
136
e : 8 ,
132
137
f : 9 ,
133
138
} ,
134
139
b : 2 ,
135
140
c : 3 ,
136
141
} ,
137
- ] ) ;
142
+ } ) ;
138
143
} ) ;
139
144
140
145
it ( 'DayObject' , ( ) => {
You can’t perform that action at this time.
0 commit comments