diff --git a/src/chronology/domain/Date.test.ts b/src/chronology/domain/Date.test.ts index 856297863..2fdacb430 100644 --- a/src/chronology/domain/Date.test.ts +++ b/src/chronology/domain/Date.test.ts @@ -86,7 +86,6 @@ describe('MesopotamianDate', () => { expect(date.ur3Calendar).toBeUndefined() }) }) - describe('converts to string', () => { it('returns the correct string representation (standard)', () => { const date = new MesopotamianDate({ @@ -347,3 +346,21 @@ describe('MesopotamianDate', () => { expect(date.toModernDate()).toBe('') }) }) + +describe('handles king date with non-numeric characters', () => { + it('parses and processes king date correctly', () => { + const kingWithDirtyDate = { + ...king, + date: 'c. 818–c. 813', + } + + const date = new MesopotamianDate({ + year: { value: '5' }, + month: { value: '3' }, + day: { value: '10' }, + king: kingWithDirtyDate, + }) + + expect(date.toModernDate()).toBe('ca. 814 BCE PJC') + }) +}) diff --git a/src/chronology/domain/DateBase.ts b/src/chronology/domain/DateBase.ts index 7630f4004..025f10aca 100644 --- a/src/chronology/domain/DateBase.ts +++ b/src/chronology/domain/DateBase.ts @@ -241,13 +241,22 @@ export class MesopotamianDateBase { year, calendar = 'Julian', }: Pick): string { - const firstReignYear = this.king?.date?.split('-')[0] + const parseKingDate = (date: string): string => { + return date.replace(/[^\d-–]/g, '') + } + + const firstReignYear = this.king?.date + ? parseKingDate(this.king.date).split(/[-–]/)[0] + : undefined + return firstReignYear !== undefined && year > 0 ? `ca. ${ parseInt(firstReignYear) - year + 1 } BCE ${calendarToAbbreviation(calendar)}` : this.king?.date && !['', '?'].includes(this.king?.date) - ? `ca. ${this.king?.date} BCE ${calendarToAbbreviation(calendar)}` + ? `ca. ${parseKingDate(this.king?.date)} BCE ${calendarToAbbreviation( + calendar + )}` : '' }