-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
deepEqual returns true for different dates #1118
Comments
@urugator i'm using MobX 2.7.0 at the moment. Does it mean i can't use |
Dates aren't plain structures, therefore they may not be structuraly comparable... |
@urugator Agree that they are not meant to be structurally compared. However they can be attributes of objects which are structurally compared. I suppose easy fix would be just to make a special case in |
Can be said about a lot of built-ins (Element, new Number, new Boolean), making a special case for each probably isn't a solution. (I agree the Date is the most common one) |
@urugator not sure there are too much builtins and handling them may not be that cumbersome (not sure Element should be treated though). And yeah, special cases are special cases, they. About expecting false: it's not obvious behavior when you compare two different objects and they are equal. Date is special (i'd say it's close to primitive type) and should be treated specially. About momentjs - that's not a special JS case so i don't think it requires special case in MobX. If it uses Date - it would benefit as well consequently. |
Maybe structural comparator should call |
Sounds like a nice idea |
Since recently |
@mweststrate |
Yep that makes sense. Can you add a link to this issue in the roadmap issue?
Op di 5 sep. 2017 om 19:46 schreef urugator <[email protected]>:
… @mweststrate <https://github.com/mweststrate>
At least for the next Mobx version, I would really consider calling
valueOf in comparer.structural.
The behavior would imho better reflect the purpose of structural
comparator, which is to treat compared things as value like ojects
<https://en.wikipedia.org/wiki/Value_object>
valueOf is a standard way to make arbitrary types comparable exactly in
this manner.
I can't imagine a situation where it would be undesirable.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1118 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhPzn8j4dsPPiZKdzZzQ7HOURAi1vks5sfYjmgaJpZM4Omcjt>
.
|
Btw it also plays well with boxed values - currently the deepEquals fails on the same boxed values (quite unexpected tbh): const Mobx = require("mobx");
const box1 = Mobx.observable(5);
const box2 = Mobx.observable(5);
console.log(Mobx.extras.deepEqual(box1, box2)); // false
console.log(Mobx.extras.deepEqual(box1.valueOf(), box2.valueOf())); // true |
Also note that MobX is just following the general consensus here on what "deep equality" means:
Also note that all api's in MobX that use comparision allow customizing the equals function that is used, so you can still define a comparer in your code base that works as you want. |
Ok, this is funny (not). Somehow this entire conversation is based on me reading: "deepEqual should return true for same dates" and
Sorry folks! Which also kinda kills the examples because I copy pasted them wrong :-/. |
Released as 3.6.0. I feel quite embarrassed for leaving this issue unsolved for halve a year but not correctly understanding it :) Also, the new algorithm is quite literally taken from underscore. Nice benefit is that dates are now treated as primitives so two Date objects with the same date yield true. |
Hello.
MobX
mobx.extras.deepEqual
returnstrue
when comparing two differentDate
objects. Probably just comparing dates deeply is meaningless but they are compared this way being attributes of some other objects.jsfiddle, example from node console:
MobX 2.70, 3.2.2. Didn't check other
Note: initially spotted when working with
moment
. When twomoment
instances created fromDate
objects are compared - they are equal as well.The text was updated successfully, but these errors were encountered: