benjamine / jsondiffpatch

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

Wrong unpatch result in swap scenario #275

Open ipip2005 opened 4 years ago

ipip2005 commented 4 years ago

In case of swapping array elements while also changing the elements that are being swapped, unpatching the diff doesn't give me the left side correctly.

The left side data

[
  {
    "name": "Argentina",
    "capital": "Buenos Aires",
    "independence": "1816-07-09T07:52:58.000Z",
    "unasur": true
  },
  {
    "name": "Chile",
    "capital": "Santiago",
    "independence": "1818-02-12T07:52:58.000Z",
    "unasur": true
  },

  {
    "name": "Brazil",
    "capital": "Brasilia",
    "independence": "1822-09-07T07:52:58.000Z",
    "unasur": true
  },
  {
    "name": "Bolivia",
    "capital": "La Paz",
    "independence": "1825-08-06T07:52:58.000Z",
    "unasur": true
  }
]

The right side data

[
  {
    "name": "Argentina",
    "capital": "Buenos Aires",
    "independence": "1816-07-09T07:52:58.000Z",
    "unasur": true
  },
  {
    "name": "Bolivia",
    "capital": "La Pbz",
    "independence": "1825-08-06T07:52:58.000Z",
    "unasur": false
  },
  {
    "name": "Brazil",
    "capital": "Brasilia",
    "independence": "1822-09-07T07:52:58.000Z",
    "unasur": true
  },
  {
    "name": "Chile",
    "capital": "Sabc",
    "independence": "1818-02-12T07:52:58.010Z",
    "unasur": true
  }
]

Here is my configuration:

{
      // used to match objects when diffing arrays, by default only === operator is used
      objectHash: (obj: any, index: number): string => { // tslint:disable-line:no-any
        return obj._id || obj.id || obj.name || '$$index:' + index;
      },
      arrays: {
          // default true, detect items moved inside the array (otherwise they will be registered as remove+add)
          detectMove: true,
          // default false, the value of items moved is not included in deltas
          includeValueOnMove: false
      },
      /**
       * Minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch.
       */
      textDiff: {
          //
          minLength: 60
      },
      /**
       * default false. if true, values in the obtained delta will be cloned
       * (using jsondiffpatch.clone by default), to ensure delta keeps no references to left or right
       * objects. this becomes useful if you're diffing and patching the same objects multiple times
       * without serializing deltas.
       * instead of true, a function can be specified here to provide a custom clone(value)
       */
      cloneDiffValues: true
    }

I calculate the diff, however unpatch the diff gave me wrong result

ipip2005 commented 4 years ago

Might be related to https://github.com/benjamine/jsondiffpatch/issues/60

dperetti commented 2 years ago

Damn, same issue. 🙁

dperetti commented 2 years ago

Swapping items 1 and 3 in an array yields wrong diff result while swapping items 1 and 4 yields a correct one...

dperetti commented 2 years ago

I can replicate it in the demo page:

Wrong swapping of 1 and 3 image

Correct swapping of 1 and 4 image

kaichii commented 5 months ago

Any updates?