Skip to content

Commit

Permalink
Simplyfy fix for date-fns breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
tpmcgowan committed Feb 18, 2024
1 parent d192ca1 commit e204b22
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
2 changes: 1 addition & 1 deletion server/utils/nunjucksSetup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('Nunjucks Filters', () => {
jest.useRealTimers()
})
;[
{ input: '2025-11-15', expected: '' }, // erroneous future date of birth
{ input: '3025-11-15', expected: '' }, // future date of birth
{ input: '2020-11-15', expected: '0 months old' },
{ input: '2020-11-14', expected: '1 month old' },
{ input: '2020-10-15', expected: '1 month old' },
Expand Down
18 changes: 5 additions & 13 deletions server/utils/nunjucksSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,14 @@ export function registerNunjucks(app?: express.Express): Environment {
return ''
}

const emptyDuration = {
years: 0,
months: 0,
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
}
const intervalDuration = intervalToDuration({ start: dob, end: today })
const duration = { ...emptyDuration, ...intervalDuration }
const duration = intervalToDuration({ start: dob, end: today })

let age = ''
if (duration.years < 1) {
age = formatDuration(duration, { format: ['months'], zero: true })
} else {
if (duration.years) {
age = formatDuration(duration, { format: ['years'] })
} else {
// workaround below for Duration zero/undefined change (https://github.com/date-fns/date-fns/issues/3658)
age = formatDuration(duration, { format: ['months'] }) || '0 months'
}

return `${age} old`
Expand Down

0 comments on commit e204b22

Please sign in to comment.