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 options passed to date-fns/parse #4474

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/date_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export function parseDate(value, dateFormat, locale, strictParsing, minDate) {
dateFormat.forEach((df) => {
let tryParseDate = parse(value, df, new Date(), {
locale: localeObject,
useAdditionalWeekYearTokens: true,
Copy link

Choose a reason for hiding this comment

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

Just pointing out the discrepancy with the option below that includes an Of in DayOfYear and not this one, in case that is not intended.

🔹 Style Consistency (Nice to have)

Image of Jacques Jacques

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These come from date-fns, that's how the options are named.

Copy link

Choose a reason for hiding this comment

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

👍

Image of Jacques Jacques

useAdditionalDayOfYearTokens: true,
});
if (strictParsing) {
strictParsingValueMatch =
Expand All @@ -95,7 +97,11 @@ export function parseDate(value, dateFormat, locale, strictParsing, minDate) {
return parsedDate;
}

parsedDate = parse(value, dateFormat, new Date(), { locale: localeObject });
parsedDate = parse(value, dateFormat, new Date(), {
locale: localeObject,
Copy link

Choose a reason for hiding this comment

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

If you'd like, and it's not a big deal, we could refactor this object into a const and use it everywhere
https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)

🔹 Code Reuse (Nice to have)

Image of Tyler A Tyler A

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's just that sometimes it gets joined with the locale, and sometimes not. We'd have to spread it everywhere ({...object} syntax), which I'm not sure is an improvement. If you think it is, I'll be happy to refactor it that way.

Copy link

Choose a reason for hiding this comment

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

That's alright, I think this was the right tradeoff.

Image of Tyler A Tyler A

useAdditionalWeekYearTokens: true,
useAdditionalDayOfYearTokens: true,
});

if (strictParsing) {
strictParsingValueMatch =
Expand All @@ -117,7 +123,10 @@ export function parseDate(value, dateFormat, locale, strictParsing, minDate) {
.join("");

if (value.length > 0) {
parsedDate = parse(value, dateFormat.slice(0, value.length), new Date());
parsedDate = parse(value, dateFormat.slice(0, value.length), new Date(), {
useAdditionalWeekYearTokens: true,
useAdditionalDayOfYearTokens: true,
});
}

if (!isValid(parsedDate)) {
Expand Down