mattphillips / deep-object-diff

Deep diffs two objects, including nested structures of arrays and objects, and returns the difference. ❄️

Home Page:https://www.npmjs.com/package/deep-object-diff

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

NanoDylan opened this issue · comments

The behavior of a detailedDiff result changed from deep-object-diff@1.1.0 and deep-object-diff@1.1.7. 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 deep-object-diff@1.1.0
{
  "added": {},
  "deleted": {
    "someArray": {}
  },
  "updated": {}
}
 */

/*
 * Results in current version deep-object-diff@1.1.7
{
  "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
    }
  }
}
 */

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 😃

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