-
Notifications
You must be signed in to change notification settings - Fork 202
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
Date range constructor automatically sorts start/end dates #201
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 lgtm, don't forget to update the CHANGELOG
|
||
if (this.end.isBefore(this.start)) { | ||
[this.start, this.end] = [this.end, this.start]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem safe to me (e.g. developer calling this makes a typo).
Double-check: have we already considered throwing an error instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for discussing 😃
IMO the library should provide the core time functionality, and the user can easily change their application logic if they're expecting a reversed time range. We classify it as a period between two points in time, rather than a directional period.
Throwing an error isn't ideal because most of the time, the functions on a range behave identically regardless of the start or end. I would think that it's far more inconvenient to require application start/end checks or error handling when they likely aren't requiring reverse time ranges? But I see the issue (if it's not documented clearly) of users not understanding what's happening.
@gf3 do you think it's better to throw error or is it unnecessary?
DateRanges should have start date before the end date, this is now automatically corrected in the constructor.
Should a user need a reverse range, they can easily change their application logic to remember that start/end is the opposite.
Closes #106