-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from colonial-heritage/date-made
Add date made to object details
- Loading branch information
Showing
5 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
apps/researcher/src/app/[locale]/objects/[id]/format-date-created.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import {formatDateCreated} from './format-date-created'; | ||
import {describe, expect, it} from '@jest/globals'; | ||
|
||
describe('formatDateCreated', () => { | ||
it('returns undefined if both startDate and endDate are undefined', () => { | ||
const formattedDate = formatDateCreated( | ||
{startDate: undefined, endDate: undefined, id: 'timeSpan'}, | ||
'en' | ||
); | ||
|
||
expect(formattedDate).toBeUndefined(); | ||
}); | ||
|
||
it('returns only startDate if endDate is undefined', () => { | ||
const formattedDate = formatDateCreated( | ||
{startDate: new Date('2021-01-01'), endDate: undefined, id: 'timeSpan'}, | ||
'en' | ||
); | ||
|
||
expect(formattedDate).toBe('Jan 1, 2021'); | ||
}); | ||
|
||
it('returns only endDate if startDate is undefined', () => { | ||
const formattedDate = formatDateCreated( | ||
{startDate: undefined, endDate: new Date('2021-01-02'), id: 'timeSpan'}, | ||
'en' | ||
); | ||
|
||
expect(formattedDate).toBe('Jan 2, 2021'); | ||
}); | ||
|
||
it('returns both startDate and endDate if both contain a date', () => { | ||
const formattedDate = formatDateCreated( | ||
{ | ||
startDate: new Date('2021-01-01'), | ||
endDate: new Date('2021-01-02'), | ||
id: 'timeSpan', | ||
}, | ||
'en' | ||
); | ||
|
||
expect(formattedDate).toBe('Jan 1 – 2, 2021'); | ||
}); | ||
|
||
it('returns one date if startDate and endDate are the same', () => { | ||
const formattedDate = formatDateCreated( | ||
{ | ||
startDate: new Date('2021-01-01'), | ||
endDate: new Date('2021-01-01'), | ||
id: 'timeSpan', | ||
}, | ||
'en' | ||
); | ||
|
||
expect(formattedDate).toBe('Jan 1, 2021'); | ||
}); | ||
|
||
it('returns only dates and not times', () => { | ||
const formattedDate = formatDateCreated( | ||
{ | ||
startDate: new Date('2021-01-01T12:00:00'), | ||
endDate: new Date('2021-01-02T13:00:00'), | ||
id: 'timeSpan', | ||
}, | ||
'en' | ||
); | ||
|
||
expect(formattedDate).toBe('Jan 1 – 2, 2021'); | ||
}); | ||
|
||
it('returns dates in the correct locale', () => { | ||
const formattedDate = formatDateCreated( | ||
{ | ||
startDate: new Date('2021-05-01'), | ||
endDate: new Date('2021-10-02'), | ||
id: 'timeSpan', | ||
}, | ||
'nl' | ||
); | ||
|
||
expect(formattedDate).toBe('1 mei – 2 okt 2021'); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
apps/researcher/src/app/[locale]/objects/[id]/format-date-created.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {TimeSpan} from '@/lib/api/definitions'; | ||
|
||
export function formatDateCreated(dateCreated: TimeSpan, locale: string) { | ||
if (!dateCreated.startDate && !dateCreated.endDate) { | ||
return undefined; | ||
} | ||
|
||
const dateTimeFormat = new Intl.DateTimeFormat(locale, { | ||
dateStyle: 'medium', | ||
}); | ||
|
||
if (!dateCreated.startDate) { | ||
return dateTimeFormat.format(dateCreated.endDate); | ||
} | ||
|
||
if (!dateCreated.endDate) { | ||
return dateTimeFormat.format(dateCreated.startDate); | ||
} | ||
|
||
return dateTimeFormat.formatRange(dateCreated.startDate, dateCreated.endDate); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c551381
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.
Successfully deployed to the following URLs:
colonial-collections-researcher – ./apps/researcher
colonial-collections-researcher-git-main-colonial-heritage.vercel.app
colonial-collections-researcher.vercel.app
colonial-collections-researcher-colonial-heritage.vercel.app
app.colonialcollections.nl
c551381
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.
Successfully deployed to the following URLs:
colonial-collections-dataset-browser – ./apps/dataset-browser
colonial-collections-dataset-browser-git-main-colonial-heritage.vercel.app
colonial-collections-dataset-browser-colonial-heritage.vercel.app
datasets.colonialcollections.nl
colonial-collections-dataset-browser.vercel.app