-
Notifications
You must be signed in to change notification settings - Fork 114
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
LocalDate to LocalDate comparison via opertors #157
Comments
Hi @paplorinc, thanks for reporting and for using js-joda. To be honest, i haven't thought about using operators with joda objects, it might be a good idea to use / implement The fact that the comparison using In summary... i think using operators to compare joda Objects is an interesting idea, would be a topic for the roadmap but is currently not in our focus. To compare js-joda objects you should use the Regards, Pattrick |
Equals (==) operator is necessary if you want to use Maps or Sets. For example:
A workaround is to store dates as strings but makes code less readable. |
Hi @paplorinc as far as i understand the es6 spec, that's a problem with the The method What about using |
You are right @pithu, |
One way to provide users a way to prevent issues with this would be to provide a set of ESLint rules would warn or error when someone tries to use a comparison operator. |
Another way is implementing a |
I would very much love @InExtremaRes's solution (#157 (comment)). Here's why. I work very hard at every company I work at to evangelize the use of this library. Then it's always very sad to say "please use js-joda, it's great! oh... but make sure that no one ever uses the The benefits of adopting the runtime assertion error with Like:
|
@dgreene1 The case against {} === {}; // false
[] === []; // false;
new Date(0) === new Date(0); // false
new Set([1]) === new Set([1]); // false
(()=>{}) === (()=>{}); // false
// ... and so on An equality check (either I think that instead of saying something like "oh... but make sure that no one ever uses the === operator since it has a bug" you should say to them "oh... BTW instances of js-joda has an On the other hand, the problem with operators like Implementing a |
Short version: Let’s please introduce the valueOf assertion like you suggested. Long version: Good point. I forgot that the comparison operators are the more problematic issue since most JS devs probably would be looking for a .equals or .isSame method. I’m polyglot (C#, JS, Perl, bash, Kotlin), so I sometimes forget nuances like this. But we should still do something about the comparison operators. |
I agree with @InExtremaRes that this is not a js-joda bug, but the way how javascript is specified. Furthermore loose comparison with type coercion is problematic and leads to a lot of new questions, eg it should be symmetric to equals and compareTo what's quite difficult, because you can start to compare different types like LocalDate with LocalDateTime, whats equal to, compare apples and oranges. Another example is, what is a comparable primitive value of a ZonedDateTime ? So implementing I am not sure if throwing an exception when calling (implicit) |
Here’s a compromise that would allow us to make it not a breaking change: If we allow the consumer to set some kind of global configuration similar to a strict mode. If jsJoda determines we’re in this new strict mode, it would throw. In that way, only people like myself and @InExtremaRes who want it can opt into the valueOf throw (or any future validation that is above and beyond the regular level of concern). |
Throwing in
I think the problem is what I said before: it works so many times to start believing it's by design. Many developers will make a few tests and will see the results are what they expect, without thinking much about that and very likely without testing corner cases. Furthermore, IMO this library could just throw, it's already type-safe in many ways that restricting the coercion to a primitive doesn't seem unaligned from his goals, even if it's not that common to throw in JS. Another option is to always return |
Good point, for developers used to the native Date behaviour the
Another good point. Let's assume we implement that valueOf throws a TypeError. For what classes should we implement it ?
|
I guess for every Probably the next question would be: If one class can have a proper So IMO (and pretending that |
@pithu @phueper I'm thinking about implementing this now since some breaking PRs have been recently merged and is very likely the next will be a major version. Is that OK? I think the way to go is make every |
@pithu, I see that @InExtremaRes's PR was merged, but when I try to use a comparison operate it does not throw. Note, I'm trying this in the example imports that you put in the demo site: Is this only occurring due to the way that it's imported into the demo site? Update: I did see that a comparison operator is no longer possible when I try this in Typescript, which is AMAZING, which means that @InExtremaRes deserves some kind of volunteer awards for fixing this issue. So maybe we can close this issue now as part of housekeeping? Hopefully after we have a clear explanation for why it's still possible in the debugger in chrome/firefox? Maybe it's that it's an ES5 import? (I'm just guessing since it seems like the PR left legacy support for things like IE11) |
The javascript on the entry page is outdated, it should be removed. Please use https://js-joda.github.io/js-joda/ to test the latest version of the library. UPDATE: i just removed the outdated lib from https://js-joda.github.io |
Hey,
We're trying out
js-joda
as amoment
alternative forTypeScript
. So far we like it, and have a question.Knowing that these return the expeced results:
etc.
(Note,
==
or===
doesn't seem to be working however...)Is it valid to compare
LocalDate
s and the rest via operators, as it seems to be working correctly, but couldn't find it in the docs (or even where thevalueOf
was overridden to allow this behavior).Thanks,
Lőrinc
The text was updated successfully, but these errors were encountered: