Skip to content
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

Merged
merged 1 commit into from Sep 12, 2019
Merged

Add new set function #1398

merged 1 commit into from Sep 12, 2019

Conversation

jmannanc
Copy link
Contributor

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.

@jmannanc
Copy link
Contributor Author

jmannanc commented Sep 2, 2019

I keep getting flow errors if I try to implement tests to check for the types. Are these tests/RangeErrors unneeded then?

Copy link
Contributor

@leshakoss leshakoss left a 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

src/set/index.js Outdated Show resolved Hide resolved
src/set/index.js Outdated Show resolved Hide resolved
src/set/index.js Outdated Show resolved Hide resolved
src/set/index.js Outdated Show resolved Hide resolved
src/set/index.js Outdated Show resolved Hide resolved
src/set/index.js Outdated Show resolved Hide resolved
src/set/index.js Outdated Show resolved Hide resolved
src/set/index.js Outdated Show resolved Hide resolved
src/set/index.js Show resolved Hide resolved
@leshakoss
Copy link
Contributor

I keep getting flow errors if I try to implement tests to check for the types. Are these tests/RangeErrors unneeded then?

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

Copy link
Contributor

@leshakoss leshakoss left a 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)
Copy link
Contributor

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)

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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?

src/set/index.js Outdated Show resolved Hide resolved
src/set/test.js Show resolved Hide resolved
src/set/index.js Outdated Show resolved Hide resolved
src/set/index.js Show resolved Hide resolved
@leshakoss
Copy link
Contributor

@sneakyfish5 I removed daysForMonth from your branch just to let travis do the full test run through the time zones, let's see what the bug is

@jmannanc
Copy link
Contributor Author

It failed, but I'm not quite sure the reason behind it failing or not/why it's only for that test.

@leshakoss
Copy link
Contributor

@sneakyfish5 ok, I added an import of setMonth like you did before and seems like the bug is gone. Let's see what will travis say about this :)

Also done some minor stylistic changes to make the code a bit more uniform with the rest of date-fns.

leshakoss
leshakoss previously approved these changes Sep 11, 2019
Copy link
Contributor

@leshakoss leshakoss left a 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! ⭐️

@jmannanc
Copy link
Contributor Author

Awesome, thank you @leshakoss for all the help. I squashed the commits for cleanup.

Copy link
Member

@kossnocorp kossnocorp left a 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feat: multi set function to set multiple parts of a date
3 participants