fix: invalid date strings

Fixes #2113
This commit is contained in:
Nolan Lawson 2022-05-14 10:27:14 -07:00
parent 78687479df
commit e3a7b783ed
1 changed files with 23 additions and 6 deletions

View File

@ -1,24 +1,41 @@
import { LOCALE } from '../_static/intl.js'
import { thunk } from './thunk.js'
export const absoluteDateFormatter = thunk(() => new Intl.DateTimeFormat(LOCALE, {
const safeFormatter = (formatter) => {
// The fediverse is wild, so invalid dates may exist. Don't fail with a fatal error in that case.
// https://github.com/nolanlawson/pinafore/issues/2113
return {
format (str) {
try {
return formatter.format(str)
} catch (e) {
if (e instanceof RangeError) {
return 'N/A'
}
throw e
}
}
}
}
export const absoluteDateFormatter = thunk(() => safeFormatter(new Intl.DateTimeFormat(LOCALE, {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
}))
})))
export const shortAbsoluteDateFormatter = thunk(() => new Intl.DateTimeFormat(LOCALE, {
export const shortAbsoluteDateFormatter = thunk(() => safeFormatter(new Intl.DateTimeFormat(LOCALE, {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
}))
})))
export const dayOnlyAbsoluteDateFormatter = thunk(() => new Intl.DateTimeFormat(LOCALE, {
export const dayOnlyAbsoluteDateFormatter = thunk(() => safeFormatter(new Intl.DateTimeFormat(LOCALE, {
year: 'numeric',
month: 'short',
day: 'numeric'
}))
})))