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

Fix - add and subtract when duration has undefined params #2515

Merged
merged 3 commits into from Jul 23, 2021

Conversation

komyg
Copy link
Contributor

@komyg komyg commented Jun 14, 2021

Hi,

I've been working with date-fns and I saw that the add and subtract functions fail when they receive a Duration argument that contains one or more params that were set explicitly as undefined.

Below is an example

import { add, sub } from 'date-fns';

const now = new Date();
const tomorrow = add(now, { days: 1, months: undefined }); // Tomorrow is invalid date
const yesterday = sub(now, { days: 1, months: undefined }); // Yesterday is invalid date

This happens, because of this check:

const months = 'months' in duration ? toInteger(duration.months) : 0

And since months is inside the duration, but is not an integer, the function fails.

I've created a PR with a proposed fix and some unit tests to validate that everything is working.

Thanks,
Komyg

@tan75 tan75 self-assigned this Jun 17, 2021
Copy link
Contributor

@tan75 tan75 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR! Much appreciated! 👍

@komyg
Copy link
Contributor Author

komyg commented Jul 6, 2021

Hi @tan75, could we merge this PR? I am not authorized to do so.

Thanks,
Komyg

@kossnocorp kossnocorp mentioned this pull request Jul 23, 2021
10 tasks
@kossnocorp kossnocorp merged commit eff8352 into date-fns:master Jul 23, 2021
@komyg komyg deleted the fix/add-subtract-undefined branch July 23, 2021 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants