@@ -211,18 +211,19 @@ class MergeDeep {
211211 }
212212 }
213213 const combined = [ ]
214- for ( const fields of Object . keys ( visited ) ) {
215- combined . push ( visited [ fields ] )
216- }
217- // Elements that are not in target are additions
218- additions [ key ] = combined . filter ( item => {
219- if ( this . isObjectNotArray ( item ) ) {
220- return ! target . some ( targetItem => GET_NAME_USERNAME_PROPERTY ( item ) === GET_NAME_USERNAME_PROPERTY ( targetItem ) )
221- } else {
222- return ! target . includes ( item )
214+ if ( Object . keys ( visited ) . length !== 0 ) {
215+ for ( const fields of Object . keys ( visited ) ) {
216+ combined . push ( visited [ fields ] )
223217 }
224- } )
225-
218+ // Elements that are not in target are additions
219+ additions [ key ] = combined . filter ( item => {
220+ if ( this . isObjectNotArray ( item ) ) {
221+ return ! target . some ( targetItem => GET_NAME_USERNAME_PROPERTY ( item ) === GET_NAME_USERNAME_PROPERTY ( targetItem ) )
222+ } else {
223+ return ! target . includes ( item )
224+ }
225+ } )
226+ }
226227 // Elements that not in source are deletions
227228 if ( combined . length > 0 ) {
228229 // Elements that not in source are deletions
@@ -247,8 +248,21 @@ class MergeDeep {
247248 this . compareDeep ( a , visited [ id ] , additions [ additions . length - 1 ] , modifications [ modifications . length - 1 ] , deletions [ deletions . length - 1 ] )
248249 }
249250 // Any addtions for the matching key must be moved to modifications
251+ const lastAddition = additions [ additions . length - 1 ]
252+ const lastModification = modifications [ modifications . length - 1 ]
253+
250254 if ( ! this . isEmpty ( additions ) ) {
251- modifications = modifications . concat ( additions )
255+ for ( const key in lastAddition ) {
256+ if ( ! lastModification [ key ] ) {
257+ lastModification [ key ] = Array . isArray ( lastAddition [ key ] ) ? [ ] : { }
258+ }
259+ if ( ! Array . isArray ( lastAddition [ key ] ) ) {
260+ Object . assign ( lastModification [ key ] , lastAddition [ key ] )
261+ } else {
262+ lastModification [ key ] . push ( ...lastAddition [ key ] )
263+ }
264+ }
265+ additions . length = 0
252266 }
253267 // Add name attribute to the modifications to make it look better ; it won't be added otherwise as it would be the same
254268 if ( ! this . isEmpty ( modifications [ modifications . length - 1 ] ) ) {
0 commit comments