Skip to content

Using for arrays? deleted seems to be wrong #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
giorgio79 opened this issue Nov 13, 2018 · 5 comments
Closed

Using for arrays? deleted seems to be wrong #43

giorgio79 opened this issue Nov 13, 2018 · 5 comments

Comments

@giorgio79
Copy link

Tried using it for arrays, but it seems deleted items are tackled incorrectly. The script is not taking the shortest path.

eg https://runkit.com/giorgio79/5bead0643e02a00012ed51d0

const { diff, addedDiff, deletedDiff, detailedDiff, updatedDiff } = require("deep-object-diff");

const wrong = [
 "Tom",
 ",",
 "was",
 "involved",
 "in"
];

const right = [
 "Tom",
 "was",
 "involved",
 "in"
 ];

console.log(detailedDiff(wrong, right));

getting

Object
added: Object {}
deleted: Object {4: undefined}
updated: Object {1: "was", 2: "involved", 3: "in"}

I would have expected
Object
added: Object {}
deleted: Object {1: ","}
updated: Object {}

@drdreo
Copy link

drdreo commented Nov 27, 2018

You are right. Even the documentation example can't distinguish deletes on the same property.

This behavior is actually quiet reasonable. How would you distinguish between an update and a delete? Maybe this is even the desired behavior in some cases (queue data structure).

const lhs = {
  foo: {
    bar: {
      a: ['a', 'b','c'],
      b: 2,
      c: ['x', 'y'],
      e: 100 // deleted
    }
  },
  buzz: 'world'
};

const rhs = {
  foo: {
    bar: {
      a: ['a', 'c'], // index 1 ('b')  deleted
      b: 2, // unchanged
      c: ['x', 'y', 'z'], // 'z' added
      d: 'Hello, world!' // added
    }
  },
  buzz: 'fizz' // updated
};

console.log(detailedDiff(lhs, rhs));

// produces: foo.bar.a.1: 'c' -->  updated
// foo.bar.a.2: undefined -->  deleted

@devniel
Copy link

devniel commented Mar 24, 2019

got the same problem. is it a bug, right ?

@anko
Copy link
Contributor

anko commented Apr 1, 2019

I think this is and should continue to be the intended behaviour.

Reasoning, following @giorgio79's example:

The operation to get from

[ "Tom", ",", "was", "involved", "in" ]

to

[ "Tom", "was", "involved", "in" ]

is to shift everything else up 1 index to cover over the ",".

That matches what the diff you see is telling you:

added: Object {}
deleted: Object {4: undefined} // delete last
updated: Object {1: "was", 2: "involved", 3: "in"} // shift everything up

If it said this,

added: Object {}
deleted: Object {1: ","}
updated: Object {}

that would imply the array should be transformed to

[ "Tom", undefined, "was", "involved", "in" ]

which is a different array!

@r3wt
Copy link

r3wt commented Apr 5, 2019

anko is correct, this is intended behavior.

@mattphillips
Copy link
Owner

Gonna close this as it is the correct behaviour as @anko explains above.

Thanks @anko for taking the time to add a clear explanation 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants