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
Add new set function #1398
Add new set function #1398
Conversation
I keep getting flow errors if I try to implement tests to check for the types. Are these tests/RangeErrors unneeded then? |
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.
@sneakyfish5 thank you for the contribution! We'd want to see this function in the library, please check the review comments out
You need to let flow know about the intended type mismatch: https://github.com/date-fns/date-fns/blob/master/src/areIntervalsOverlapping/test.js#L166 |
6d8b4de
to
620479d
Compare
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.
@sneakyfish5 Thanks a lot! 🙏 I've got some new comments and nitpicks, please check them out
src/set/index.js
Outdated
} | ||
|
||
if (year > 0) date.setFullYear(year) | ||
if (month != null) date.setMonth(toInteger(month), days) |
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.
I meant this not only for months but for every value. Like for instance you could have negative years (ie years BC) and they will be treated by this function just like null
.
What I think we need to do is to check for nullability and set the value right away for every value instead of the useless > 0
intermediate check.
I suggest consolidating it all like this:
if (values.year != null) {
date.setFullYear(toInteger(values.year))
}
if (values.month != null) {
date.setMonth(toInteger(values.month))
}
if (values.date != null) {
date.setDate(toInteger(values.date))
}
and so on. (Braces are fine, they don't affect the bundle size)
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.
Fixed, however I needed to keep days for the setMonth otherwise it would create an error where the resulting month is a month higher than it is supposed to be. Maybe my implementation is wrong, however I saw this happening date-fns setMonth
function as well, so I opted to leave it in there unless you have any objection.
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.
Not sure what do you mean. I checked the branch out locally and removed daysForMonth
and tests pass, also in the console it looks normal:
> const set = require('./src/set')
> set(new Date(), {month: 0}).toString()
'Sun Jan 06 2019 13:24:19 GMT+0100 (Central European Standard Time)'
What am I missing?
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.
Huh, I'm not too sure. When I run the tests on my local with daysForMonth
removed I end up failing this test, and using daysForMonth
as the extra parameter seemed to fix it.
× set the date with every option
Chrome 76.0.3809 (Windows 10.0.0)
AssertionError: # src\set\test.js:18
assert.deepEqual(result, new Date(2014, 8, 20, 12, 12, 12, 12))
| |
| new Date("2014-09-20T17:12:12.012Z")
new Date("2014-10-20T17:12:12.012Z")
Maybe I've messed up something on my end?
94b8192
to
d0e1194
Compare
@sneakyfish5 I removed |
It failed, but I'm not quite sure the reason behind it failing or not/why it's only for that test. |
@sneakyfish5 ok, I added an import of Also done some minor stylistic changes to make the code a bit more uniform with the rest of date-fns. |
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.
@sneakyfish5 alright I think it's ready to be merged. Thanks a lot for your contributon! ⭐️
Awesome, thank you @leshakoss for all the help. I squashed the commits for cleanup. |
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.
Looks good, thank you a lot!
Fix #1388
Moment.js also has a function like this one so I thought it'd be good to add, and may be easier for some people to use.
This is my first contribution so do let me know if I did anything wrong and I'll be happy to fix it.