benjamine / jsondiffpatch

Diff & patch JavaScript objects
MIT License
4.78k stars 466 forks source link

Js Array diff problem #290

Open pcelica88 opened 4 years ago

pcelica88 commented 4 years ago

I noticed that there is a problem with comparing arrays. The problem only occurs:

Example

var left = {"ids": [1, 2, 3]}; var right = {"ids": [4, 1, 2, 3]}; var delta = jsondiffpatch.diff(left, right); Results In this case only the replacement of the first element will be detected, and what I need is completely added new element without replacing first one.

Can you please check if this is a bug or this should work like this. Thanks

fw623 commented 4 years ago

It anyone is wondering, this is just a bug/misleading on the demo page.

It correctly patches it:

const left = { "ids": [1, 2, 3] }
const right = { "ids": [4, 1, 2, 3] }

const delta = jsondiffpatch.diff(left, right) // { ids: { '0': [ 4 ], _t: 'a' } }
const reconstructedRight = jsondiffpatch.patch(left, delta) // { ids: [ 4, 1, 2, 3 ] }