mattphillips / deep-object-diff

Deep diffs two objects, including nested structures of arrays and objects, and returns the difference. ❄️
https://www.npmjs.com/package/deep-object-diff
MIT License
1.05k stars 89 forks source link

Fix the preserve array function with array of objects #44

Open popojargo opened 5 years ago

popojargo commented 5 years ago

Your examples with works fine execpt when the values compared in the array are objects.

For example. this will work fine:

// added (end of array)
const left = { alphabet: ['a', 'b', 'c'] };
const right = { alphabet: ['a', 'b', 'c', 'd'] };
diff(left, right);
/*
{
  alphabet: [empty, empty, empty, 'd']
}
*/

But, if you use a similar representation with objects, it fails:

const left = { alphabet: [{ a: 'a' }, { b: 'b' }, { c: 'c' }] };
const right = { alphabet: [{ a: 'a' }, { b: 'b' }, { c: 'c' }, { d: 'd' }] };
coveralls commented 5 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 0a408004c1db579ac0b702f9ded4e953db1694d9 on popojargo:fix/preserve-array into 6296889cfc0e54f08b0077460c9bf75f2febfa9f on mattphillips:master.

popojargo commented 5 years ago

@mattphillips Can you take a look a this?

anko commented 5 years ago

Hi, user here. Drivebying with a lightning review.

Things that made me think:

Hopefully this is helpful :running_man:

popojargo commented 5 years ago

@anko

  1. I'm using the "preserve-array" functions that is available in "src/preserve-array.js". I'm not sure if it's officially suppported but that's what the function that I need to diff my objects.

Basically, I'm using CouchDB has a database. When updating a document, I need to do a partial update in order to update only the fields that have changed. When manipulating objects with diff, it works fine. Although, the array handling by default is not very good.

For this reason, I use preserve-array.js which keeps the array and produce a real "array difference".

So let's say I had originally this document:

{
  "children": [{"name":1}]
}

and I end up with this document:

{
  "children": [{"name":1},{"name":2}]
}

I would like the following diff to apply to the latest document:

{
  "children": [empty,{"name":2}]
}
  1. Using the "preserve-array" works fine with array of primitives (number, string). When I'm using arrays of objects, the code just crash since the lhs or rhs can be null at some point.

  2. Yes I did change the tests but the changes I made are testing the object diff. Since a lot of "test cases" were covered in a single test case, I simply added the new things to cover. I can make it more explicit if required.

bdombro commented 1 year ago

Speaking of which -- why is preserve-array in the repo when it appears unused? And it's also undocumented.