-
Notifications
You must be signed in to change notification settings - Fork 1
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: improve date parsing for collection items #732
Changes from 5 commits
6fe440e
df8e63f
3b67c53
390ee8e
2daaaf2
715e22c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import dayjs from "dayjs" | ||
import customParseFormat from "dayjs/plugin/customParseFormat" | ||
import { format } from "date-fns" | ||
|
||
dayjs.extend(customParseFormat) | ||
import type { FormattedDate } from "~/types" | ||
import { getParsedDate } from "./getParsedDate" | ||
|
||
// Standardise the format of dates displayed on the site | ||
export const getFormattedDate = (date: string) => | ||
dayjs(date, [ | ||
"DD/MM/YYYY", | ||
"D MMM YYYY", | ||
"DD MMM YYYY", | ||
"YYYY-MM-DDTHH:mm:ss.SSSZ", | ||
]).format("D MMMM YYYY") | ||
export const getFormattedDate = (dateString?: string): FormattedDate => { | ||
if (dateString === undefined) { | ||
return format(new Date(), "d MMMM yyyy") as FormattedDate | ||
} | ||
|
||
const date = getParsedDate(dateString) | ||
|
||
return format(date, "d MMMM yyyy") as FormattedDate | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { isMatch, parse } from "date-fns" | ||
|
||
const SUPPORTED_DATE_FORMATS = [ | ||
"dd/MM/yyyy", | ||
"d MMM yyyy", | ||
"d MMMM yyyy", | ||
"dd MMM yyyy", | ||
"dd MMMM yyyy", | ||
"yyyy-MM-dd", | ||
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", | ||
] | ||
|
||
export const getParsedDate = (dateString: string) => { | ||
try { | ||
const parsedDate = SUPPORTED_DATE_FORMATS.reduce<Date | undefined>( | ||
(acc, format) => { | ||
if (acc) { | ||
// Date has already been parsed by an earlier format | ||
return acc | ||
} | ||
|
||
if (isMatch(dateString, format)) { | ||
return parse(dateString, format, new Date()) | ||
} | ||
Comment on lines
+22
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we should shift the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 715e22c. |
||
|
||
return acc | ||
}, | ||
undefined, | ||
) | ||
|
||
if (parsedDate) { | ||
return parsedDate | ||
} | ||
|
||
return new Date() | ||
} catch (e) { | ||
return new Date() | ||
} | ||
} |
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.
actually the flow i had in mind was
reduce(function, new Date()
then in our reducer function:
and we can put supported date formats in increasing order of priority, so that the code looks cleaner overall. not a big deal though, just that this was not quite what i had in mind
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.
Hmm I a bit confused, I think that's what I'm doing right now? The reducer function is indeed going by priority (from first to last element) and it starts with
undefined
to differentiate between whether the date string has been parsed or not. If parsed then the type is of Date, otherwise it will remain as undefined.