jsondiffpatch icon indicating copy to clipboard operation
jsondiffpatch copied to clipboard

Superfluous change detected for simple swap/reverse case

Open CoDEmanX opened this issue 11 years ago • 3 comments

If you diff {"reverse": [1, 5, 9]} with {"reverse": [9, 5, 1]}, two changes are reported:

move from index 2 to index 0

correct.

move from index 1 to index 1

Why?!

Seems to happen if there is an odd number of elements only.

CoDEmanX avatar Aug 24 '14 19:08 CoDEmanX

it looks superflous but actually isn't (note you can't really do this in 1 movement, if you move 9 to index 0 the result is [9, 1, 5]. The reason is the movement indices are noted as "index at left state" => "index at right state" (so a 1 at the left, is not exactly the same as 1 at right, yes, sounds confusing). It becomes clear seeing how array items movements are performed (when patching), it happens in this order:

  • items are removed from their original (left) positions
  • items are inserted in their final (right) positions

in this example, the sequence would be like this:

  • 9 is removed from index 2, result: [1, 5]
  • 5 is removed from index 1, result: [1]
  • '9' is inserted at index 0, result: [9, 1]
  • 5 is inserted at index 1, result: [9, 5, 1]

benjamine avatar Aug 24 '14 20:08 benjamine

I see. But still, there shouldn't be an arrow from and to the very same item in the visualization, should there?

CoDEmanX avatar Aug 24 '14 20:08 CoDEmanX

mm I guess the visualization is not clearest for these cases, as it doesn't represents the temporal intermediate steps. maybe arrows with identical start and end indices could be hidden, but I'm not sure, as there is something actually happening...

benjamine avatar Aug 24 '14 20:08 benjamine