Skip to content

detailedDiff behavior when the last item in an array is deleted, breaking change between 1.1.0 and 1.1.7 #75

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
NanoDylan opened this issue Feb 16, 2022 · 2 comments

Comments

@NanoDylan
Copy link

The behavior of a detailedDiff result changed from [email protected] and [email protected]. Based on my interpretation of the README.md example for detailedDiff, neither 1.1.0 nor 1.17 behavior matches the documentation ( See issue #74 ).
In version 1.1.0, if the last item in the array is deleted, then the change is indicated in the deleted section of the diff results.
In the current version, 1.1.7, if the last item in the array is deleted, then the change is indicated in both the deleted section and the updated section of the diff results.

const { detailedDiff } = require('deep-object-diff');

const lhs = {
  someArray: [ 'a'
  ]
};

const rhs = {
  someArray: [
  ]
};

const objectDifferences = detailedDiff(lhs, rhs);
console.log(JSON.stringify(objectDifferences, null, 2));

/*
 * Results in [email protected]
{
  "added": {},
  "deleted": {
    "someArray": {}
  },
  "updated": {}
}
 */

/*
 * Results in current version [email protected]
{
  "added": {},
  "deleted": {
    "someArray": {}
  },
  "updated": {
    "someArray": {}
  }
}
 */

/*
 * Expected results based on my interpretation of https://github.com/mattphillips/deep-object-diff#detaileddiff
{
  "added": {},
  "deleted": {
    "someArray": {
      '0': undefined
    }
  }
}
 */
@mattphillips
Copy link
Owner

Hi @NanoDylan you're correct there has been a change in behaviour around diff and updatedDiff (which is used in detailedDiff) when comparing a nested value that has become an empty object / array. This change is a bugfix for: #66

Previously deep-object-diff would incorrectly consider values on the rhs of the diff that are empty to have no difference but in actuality they do.

Take your example:

const lhs = { someArray: [ 'a'] };
const rhs = { someArray: [] };

updatedDiff(lhs, rhs) // -> { someArray: {} }

The someArray key in the right hand object has been updated to an empty Array. Which in DoD is represented as an empty object.


I'll close this for now too but again feel free to re-open / ping me if I've misunderstood or have got anything wrong above 😃

@NanoDylan
Copy link
Author

Perfect. 😄 I understand the logic of the #66 bugfix. I will update accordingly. Thanks!

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

2 participants