Skip to content

Diff gives strange results when passing invalid values #29

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
niklas-e opened this issue Feb 27, 2018 · 4 comments
Closed

Diff gives strange results when passing invalid values #29

niklas-e opened this issue Feb 27, 2018 · 4 comments

Comments

@niklas-e
Copy link

It seems to be somewhat unintuitive to return differences when passing non-object values. For example, if comparing an object to a number, it will return the number as a result. Same goes for undefined, null and string (and most likely all other types). I think it should throw an error or return an empty object when comparing non-object values,

Here's some tests: https://runkit.com/niklas-e/5a95669e6ac1ed0012f9ebbd

cc: @SamiEklund

@mattphillips
Copy link
Owner

Hey @niklas-e I'm not sure about this one.

I agree the outputs in your example look odd (it is just returning the right hand side when the values are not the same).

I think this is potentially a user land check, I'm not sure that it is semantic to return an empty object as the difference when called with primitives and I wouldn't want to throw an error.

I think if the use case is that the value could be something other than an object then you may just want to wrap the call to deep-object-diff in a check to see the arguments are both objects and then you can put your own semantics around the this case.

@niklas-e
Copy link
Author

@mattphillips, fair enough. Though I still think there could be sanity checks for undefined and null inside of deep-object-diff. For example when passing diff({ prop: true }, undefined) it returns undefined, but when passing diff(undefined, { prop: true }) it returns { prop: true }. Same goes for null. Also when passing diff(null, null) or diff(undefined, undefined) it returns an empty object.

After all at least undefined is quite common and beforementioned behavior might cause confusion. Otherwise using diff would always require a wrapper with undefined checking.

@mattphillips
Copy link
Owner

If we look at your examples they all make sense to me, with one assumption: deep-object-diff represents no diff as an empty object.

diff({ prop: true }, undefined)

What are the differences between the original value of an object and the updated value of undefined? The value has become undefined.

diff(undefined, { prop: true })

What are the difference between the original value of underfined and the updated value of an object? The value has become an object.

diff(undefined, undefined)

What is different between undefined and undefined? There is no difference so use deep-object-diff’s empty object to represent equality.

The same stands for any primitives, including null.

So it wouldn’t make sense to say there is no difference between an object and a primitive, when there is.

That’s why if in user land you may pass in a primitive and don’t want to receive the real difference between the primitive and an object or vice verse then you need to do a sanity check as that logic is specific to your use case.

I hope that all makes sense 😆

@mattphillips
Copy link
Owner

Hey @niklas-e I'm going to go ahead and close this issue, feel free to re-open if you disagree with the points I have made above 😄

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