Skip to content

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

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
LAITONEN opened this issue Feb 26, 2018 · 4 comments
Closed

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

LAITONEN opened this issue Feb 26, 2018 · 4 comments

Comments

@LAITONEN
Copy link

LAITONEN commented Feb 26, 2018

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
Copy link
Owner

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
Copy link
Owner

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

@LAITONEN
Copy link
Author

@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
Copy link
Owner

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

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