chbrown / rfc6902

Complete implementation of RFC6902 in TypeScript
https://chbrown.github.io/rfc6902/
322 stars 39 forks source link

createPatch wrong when switching array elements #4

Closed LennartC closed 9 years ago

LennartC commented 9 years ago
var patches = rfc6902.createPatch([ "A", "B" ], [ "B", "A" ]); // produces:
{"op":"add","path":"/0","value":"B"}    Patch 0
{"op":"remove","path":"/1"} Patch 1

And this is wrong. The first patch makes the array look like:

 [ "B", "A", "B" ]

And because the patches are sequential, the second patch will remove A, making it look like

[ "B", "B" ]

So the second patch should be:

{"op":"remove","path":"/2"}
dhritzkiv commented 9 years ago

Looks like this could be related to #3 (my issue), as it relates to arrays, and doesn't necessarily produce a patch that results in the desired outcome.

LennartC commented 9 years ago

You are correct, it's the same issue as #3 . And where you say:

/*
`diff` is equal to:
 [ 
    { op: 'remove', path: '/arr/1' },
    { op: 'remove', path: '/arr/2' }
 ]
which is correct
*/

It is actually wrong. It should be:

 [ 
    { op: 'remove', path: '/arr/1' },
    { op: 'remove', path: '/arr/1' }
 ]
dhritzkiv commented 9 years ago

@LennartC Good catch! I'll update my issue.

chbrown commented 9 years ago

Thanks for the bug report. Fixed in v1.0.5. See my comment in #3 for explanation.