flitbit / diff

Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
MIT License
3k stars 214 forks source link

FeatureRequest: change the diff of array elements (or add such an option) #78

Closed zatziky closed 6 years ago

zatziky commented 8 years ago

For our auditing system we needed to persist the whole array element and not just the difference.

The object difference for

const objA = {array: [{a: 1}]};
const objB = {array: [{a: 2}]};

would look in the original deep-diff like:

[{
  kind: "E",
  lhs: 1,
  rhs: 2,
  path: ["array", 0, "a"]
}]

Our need is that the result keeps the original and changed elements. It also has the kind as A and changes the structure a bit:

[{
   kind: "A",
   path: ["array", 0],
   item: {
       kind: "E",
       path: ["a"],
       elementLeft: {a: 1},
       elementRight: {a: 2},
       lhs: 1,
       rhs: 2
   }
}]

You could add an option to change the default behavior so that it creates diffs of edited array elements in the proposed manner. I've made a small project deepdiff-keep-element where you can check our intentions in tests.

flitbit commented 7 years ago

I like the proposal, it basically rolls up (or short-circuits) the changes for each array element. I can see that this may be handy for some use cases. I also like the ramda library you're using - very cool. I try not to use any dependencies at all, that's why the code is so long-winded.

Here's my take: some use cases may benefit from different difference-reporting structures, therefore, it would be nice if deep-diff had a pluggable difference-reporting mechanism.

With such a mechanism, many implementations are possible, including mine (default) and yours.

TheScree commented 6 years ago

I'd second implementation of this suggestion, I've found myself needing to know which elements inside an object's attribute (which happens to be an array of values) has changed.

Array parsing feels important to be honest. You aren't just telling users that a difference existed, you've taken the steps to list out exactly what changed. I'd like to see this library fully embrace that role.

flitbit commented 6 years ago

This issue has changed as the structure of the differences has changed. I added an issue-78 which shows an easy, user implemented solution using JSON-Pointers.