Skip to content

Commit

Permalink
Finalize ranges & update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Mar 4, 2024
1 parent 65b2541 commit 1b4e9ec
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 62 deletions.
10 changes: 5 additions & 5 deletions src/chronology/domain/Date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('MesopotamianDate', () => {
isSeleucidEra: true,
})
expect(date.toString()).toBe(
'∅.V.10 SE (ca. 19 August 302 BCE PJC | ca. 14 August 302 BCE PGC)'
'∅.V.10 SE (ca. 19 August - 16 September 302 BCE PJC | ca. 14 August - 11 September 302 BCE PGC)'
)
})

Expand All @@ -131,7 +131,7 @@ describe('MesopotamianDate', () => {
isSeleucidEra: true,
})
expect(date.toString()).toBe(
'12.∅.10 SE (ca. 4 May 302 BCE PJC | ca. 29 April 302 BCE PGC)'
'12.∅.10 SE (ca. 4 May 302 - 24 March 301 BCE PJC | ca. 29 April 302 - 20 March 301 BCE PGC)'
)
})

Expand Down Expand Up @@ -167,7 +167,7 @@ describe('MesopotamianDate', () => {
isSeleucidEra: false,
})
expect(date.toString()).toBe(
'∅.V.10 Darius I (ca. 31 July 512 BCE PJC | ca. 25 July 512 BCE PGC)'
'∅.V.10 Darius I (ca. 31 July - 29 August 512 BCE PJC | ca. 25 July - 23 August 512 BCE PGC)'
)
})

Expand All @@ -180,7 +180,7 @@ describe('MesopotamianDate', () => {
isSeleucidEra: false,
})
expect(date.toString()).toBe(
'12.∅.10 Darius I (ca. 16 April 512 BCE PJC | ca. 10 April 512 BCE PGC)'
'12.∅.10 Darius I (ca. 16 April 512 - 6 March 511 BCE PJC | ca. 10 April 512 - 28 February 511 BCE PGC)'
)
})

Expand All @@ -193,7 +193,7 @@ describe('MesopotamianDate', () => {
isSeleucidEra: false,
})
expect(date.toString()).toBe(
'∅.V.10 Darius I (ca. 31 July 512 BCE PJC | ca. 25 July 512 BCE PGC)'
'∅.V.10 Darius I (ca. 31 July - 29 August 512 BCE PJC | ca. 25 July - 23 August 512 BCE PGC)'
)
})

Expand Down
65 changes: 37 additions & 28 deletions src/chronology/domain/DateBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ export class MesopotamianDateBase {
this.isSeleucidEra = isSeleucidEra
this.isAssyrianDate = isAssyrianDate
this.ur3Calendar = ur3Calendar
if (this.getEmptyOrBrokenFields().length > 0 && this.getType() !== null) {
if (
this.getEmptyFields().length > 0 &&
[DateType.nabonassarEraDate, DateType.seleucidDate].includes(
this.dateType as DateType
)
) {
this.range = DateRange.getRangeFromPartialDate(this)
console.log('!! Range:', this.range, this.range.toDateString())
}
}

Expand All @@ -117,7 +121,7 @@ export class MesopotamianDateBase {
return !!this.king?.date
}

getType(): DateType | null {
get dateType(): DateType | null {
if (this?.year?.value && this.isSeleucidEraApplicable(this?.year?.value)) {
return DateType.seleucidDate
} else if (this.isNabonassarEraApplicable()) {
Expand All @@ -131,14 +135,11 @@ export class MesopotamianDateBase {
}

toModernDate(calendar: 'Julian' | 'Gregorian' = 'Julian'): string {
const type = this.getType()
const type = this.dateType
if (type === null) {
return ''
}
const dateProps = {
...this.getDateApproximation(),
calendar,
}
const dateProps = this.getDateProps(calendar)
const { year } = dateProps
return {
seleucidDate: () => this.seleucidToModernDate(dateProps),
Expand All @@ -157,34 +158,31 @@ export class MesopotamianDateBase {
calendar = 'Julian',
}: Pick<DateProps, 'calendar'>): string {
return `ca. ${this.eponym?.date} BCE ${calendarToAbbreviation(calendar)}`
// ToDo: Continue here
// Calculate years in range if year is missing
}

private getDateApproximation(): {
private getDateProps(
calendar: 'Julian' | 'Gregorian' = 'Julian'
): {
year: number
month: number
day: number
isApproximate: boolean
calendar: 'Julian' | 'Gregorian'
} {
const year = parseInt(this.year.value)
const month = parseInt(this.month.value)
const day = parseInt(this.day.value)
const isApproximate = this.isApproximate()
// ToDo: Change this
return {
year: isNaN(year) ? -1 : year,
month: isNaN(month) ? 1 : month,
day: isNaN(day) ? 1 : day,
isApproximate,
year: parseInt(this.year.value) ?? -1,
month: parseInt(this.month.value) ?? 1,
day: parseInt(this.day.value) ?? 1,
isApproximate: this.isApproximate(),
calendar,
}
}

getEmptyOrBrokenFields(): Array<'year' | 'month' | 'day'> {
getEmptyFields(): Array<'year' | 'month' | 'day'> {
const fields: Array<'year' | 'month' | 'day'> = ['year', 'month', 'day']
return fields
.map((field) => {
if (isNaN(parseInt(this[field].value)) || this[field].isBroken) {
if (isNaN(parseInt(this[field].value))) {
return field
}
return null
Expand Down Expand Up @@ -220,8 +218,10 @@ export class MesopotamianDateBase {
isApproximate,
calendar,
}: DateProps): string {
// ToDo: Continue here
// Use converter to compute earliest and latest dates in range
const dateRangeString = this.getDateRangeString(calendar)
if (dateRangeString) {
return dateRangeString
}
const converter = new DateConverter()
converter.setToSeBabylonianDate(year, month, day)
return this.insertDateApproximation(
Expand All @@ -238,8 +238,10 @@ export class MesopotamianDateBase {
calendar,
}: DateProps): string {
if (this.kingName) {
// ToDo: Continue here
// Use converter to compute earliest and latest dates in range
const dateRangeString = this.getDateRangeString(calendar)
if (dateRangeString) {
return dateRangeString
}
const converter = new DateConverter()
converter.setToMesopotamianDate(this.kingName, year, month, day)
return this.insertDateApproximation(
Expand All @@ -250,6 +252,15 @@ export class MesopotamianDateBase {
return ''
}

getDateRangeString(calendar: 'Julian' | 'Gregorian'): string | undefined {
if (this.range !== undefined) {
return this.insertDateApproximation(
this.range.toDateString(calendar),
true
)
}
}

get kingName(): string | undefined {
return Object.keys(data.rulerToBrinkmanKings).find(
(key) => data.rulerToBrinkmanKings[key] === this.king?.orderGlobal
Expand All @@ -268,8 +279,6 @@ export class MesopotamianDateBase {
: this.king?.date && !['', '?'].includes(this.king?.date)
? `ca. ${this.king?.date} BCE ${calendarToAbbreviation(calendar)}`
: ''
// ToDo: Continue here
// Calculate years in range if year is missing
}

private insertDateApproximation(
Expand Down
61 changes: 32 additions & 29 deletions src/chronology/domain/DateRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,32 @@ export default class DateRange {
const startDateString = this._converter.toDateString(calendarType)
this._converter.setToCjdn(this.end.cjdn)
const endDateString = this._converter.toDateString(calendarType)
return `${this.getCompactStartDateString(
startDateString,
endDateString
)} - ${endDateString}`
}

private getCompactStartDateString(
startDateString: string,
endDateString: string
): string {
let startDateArray: string[] = []
const endDateArray = endDateString.split(' ').reverse()
const startDateArray: string[] = []
for (const [index, startElement] of startDateString
.split(' ')
.reverse()
.entries()) {
const _startDateArray = startDateString.split(' ').reverse()
for (const [index, startElement] of _startDateArray.entries()) {
const endElement = endDateArray[index]
if (startElement !== endElement) {
startDateArray.push(startElement)
startDateArray = _startDateArray.slice(index)
break
}
}
// ToDo: Continue here. Verify that it works as expected
return `${startDateArray.reverse().join(' ')} - ${endDateString}`
return startDateArray.reverse().join(' ')
}

private setToPartialDate(date: MesopotamianDateBase): void {
const fieldsToUpdate = date.getEmptyOrBrokenFields()
const fieldsToUpdate = date.getEmptyFields()
const defaultValues = this.getDefaultDateValues(date)

const startDate = this.calculateDateValues(
fieldsToUpdate,
defaultValues,
Expand All @@ -49,7 +56,6 @@ export default class DateRange {
'end',
date
)

this.setDateBasedOnType(date, startDate, 'start')
this.setDateBasedOnType(date, endDate, 'end')
}
Expand Down Expand Up @@ -86,38 +92,37 @@ export default class DateRange {
private setDateBasedOnType(
date: MesopotamianDateBase,
dateValues: { year: number; month: number; day: number },
type: 'start' | 'end'
position: 'start' | 'end'
): void {
if (date.getType() === DateType.seleucidDate) {
if (date.dateType === DateType.seleucidDate) {
this._converter.setToSeBabylonianDate(
dateValues.year,
dateValues.month,
dateValues.day
)
} else if (date.getType() === DateType.nabonassarEraDate) {
} else if (date.dateType === DateType.nabonassarEraDate) {
this._converter.setToMesopotamianDate(
date.kingName as string,
dateValues.year,
dateValues.month,
dateValues.day
)
}

this[type] = { ...this._converter.calendar }
this[position] = { ...this._converter.calendar }
}

private getEndValueForPartialDate(
date: MesopotamianDateBase,
field: 'year' | 'month' | 'day'
): string {
const dateType = date.getType()
const { dateType } = date
if (dateType === DateType.seleucidDate) {
return {
year: () => `${this.seleucidRangeEndYear}`,
month: () => `${this.getSeleucidDateEndMonth(date)}`,
day: () => `${this.getSeleucidDateEndDay(date)}`,
}[field]()
} else if (DateType.nabonassarEraDate) {
} else if (dateType === DateType.nabonassarEraDate) {
return {
year: () => `${this.getNabonassarRangeEndYear}`,
month: () => `${this.getNabonassarDateEndMonth(date)}`,
Expand All @@ -132,42 +137,40 @@ export default class DateRange {
}

private getSeleucidRangeEndYear(date: MesopotamianDateBase): number {
return date.getEmptyOrBrokenFields().includes('year')
return date.getEmptyFields().includes('year')
? this.seleucidRangeEndYear
: parseInt(date.year.value)
}

private getSeleucidDateEndMonth(date: MesopotamianDateBase): number {
const year = this.getSeleucidRangeEndYear(date)
return date.getEmptyOrBrokenFields().includes('month')
? this._converter.getMesopotamianMonthsOfSeYear(year).length
: parseInt(date.month.value)
return date.getEmptyFields().includes('month')
? this._converter.getMesopotamianMonthsOfSeYear(year).slice(-1)[0].value
: parseInt(date.month.value) ?? 12
}

private getSeleucidDateEndDay(date: MesopotamianDateBase): number {
const year = this.getSeleucidRangeEndYear(date)
const month = this.getSeleucidDateEndMonth(date)
this._converter.setToSeBabylonianDate(year, month, 1)
return this._converter.calendar.mesopotamianMonthLength ?? 29
return this._converter.calendar.mesopotamianMonthLength ?? 28
}

private get nabonassarRangeEndYear(): number {
return this._converter.latestDate.regnalYears ?? 1
}

private getNabonassarRangeEndYear(date: MesopotamianDateBase): number {
return date.getEmptyOrBrokenFields().includes('year')
return date.getEmptyFields().includes('year')
? this.nabonassarRangeEndYear
: parseInt(date.year.value)
}

private getNabonassarDateEndMonth(date: MesopotamianDateBase): number {
const year = this.getNabonassarRangeEndYear(date)
this._converter.setToMesopotamianDate(date.kingName as string, year, 1, 1)
return date.getEmptyOrBrokenFields().includes('month')
? this._converter.getMesopotamianMonthsOfSeYear(
this._converter.calendar.seBabylonianYear
).length
return date.getEmptyFields().includes('month')
? this._converter.getMesopotamianMonthsOfSeYear(year).slice(-1)[0].value
: parseInt(date.month.value)
}

Expand All @@ -180,7 +183,7 @@ export default class DateRange {
month,
1
)
return this._converter.calendar.mesopotamianMonthLength ?? 29
return this._converter.calendar.mesopotamianMonthLength ?? 28
}

static getRangeFromPartialDate(date: MesopotamianDateBase): DateRange {
Expand Down

0 comments on commit 1b4e9ec

Please sign in to comment.