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

Nested empty object with no change counts as a change #28

Closed LAITONEN closed 6 years ago

LAITONEN commented 6 years ago

If I pass the same object, which nests another object with no properties as a child, as both arguments of diff or detailedDiff, it will show that the change occurred and the change is an empty object.

const obj = { subObj: {} }
const difference = diff(obj, obj)
console.log(difference) // { subObj: {} }

In my application I needed to do this to circumvent the issue:

const difference = detailedDiff(objectBefore, objectAfter)
let changeOccured = Object.keys(difference).length > 0
if (changeOccured) {
    for (let change in difference) {
        if (typeof difference[change] == 'object' && Object.keys(difference[change]).length == 0) {
            changeOccured = false
        } else changeOccured = true
    }
 }

P.S. With detailedDiff this kind of 'change' is counted as "added".

mattphillips commented 6 years ago

Hey @LAITONEN which version of deep-object-diff are using to see this issue?

I've tried to reproduce it but can't seem to, would you mind creating a repo with an example of the issue?

Here's what I get when I try to reproduce using v1.1.0:

screen shot 2018-02-26 at 17 32 21
mattphillips commented 6 years ago

Ah just seen you have closed this so I assume its working :smile:

LAITONEN commented 6 years ago

@mattphillips I resolved my issue, but, actually, there is an improvement that I would like to propose. The problem was that in one case subObj === undefined, in another subObj === {}. This caused diff to assume that the change has occurred.

Do you think this should be considered as "no change"?

mattphillips commented 6 years ago

Hey, I think it’s correct that they are treated as being different as dod doesn’t place any bias on the values supplied and fundamentally an object is a different value to undefined