Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mesopotamian date #381

Merged
merged 18 commits into from
Aug 8, 2023
2 changes: 1 addition & 1 deletion src/common/BrinkmanKings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import BrinkmanKings from 'common/BrinkmanKings.json'
import { Popover } from 'react-bootstrap'
import HelpTrigger from 'common/HelpTrigger'

interface King {
export interface King {
orderGlobal: number
groupWith?: number
dynastyNumber: string
Expand Down
1 change: 1 addition & 0 deletions src/fragmentarium/application/FragmentService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const fragmentRepository = {
fetchGenres: jest.fn(),
updateGenres: jest.fn(),
updateScript: jest.fn(),
updateDate: jest.fn(),
fetchPeriods: jest.fn(),
updateReferences: jest.fn(),
folioPager: jest.fn(),
Expand Down
8 changes: 8 additions & 0 deletions src/fragmentarium/application/FragmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import produce, { castDraft } from 'immer'
import { ManuscriptAttestation } from 'corpus/domain/manuscriptAttestation'
import { FragmentQuery } from 'query/FragmentQuery'
import { QueryResult } from 'query/QueryResult'
import { MesopotamianDate } from 'fragmentarium/domain/Date'

export const onError = (error) => {
if (error.message === '403 Forbidden') {
Expand Down Expand Up @@ -57,6 +58,7 @@ export interface FragmentRepository {
fetchPeriods(): Bluebird<string[]>
updateGenres(number: string, genres: Genres): Bluebird<Fragment>
updateScript(number: string, script: Script): Bluebird<Fragment>
updateDate(number: string, date: MesopotamianDate): Bluebird<Fragment>
updateTransliteration(
number: string,
transliteration: string
Expand Down Expand Up @@ -143,6 +145,12 @@ export class FragmentService {
.then((fragment: Fragment) => this.injectReferences(fragment))
}

updateDate(number: string, date: MesopotamianDate): Bluebird<Fragment> {
return this.fragmentRepository
.updateDate(number, date)
.then((fragment: Fragment) => this.injectReferences(fragment))
}

fetchGenres(): Bluebird<string[][]> {
return this.fragmentRepository.fetchGenres()
}
Expand Down
183 changes: 183 additions & 0 deletions src/fragmentarium/domain/Date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
//import produce, { castDraft, Draft, immerable } from 'immer'
//import { immerable } from 'immer'
//import _ from 'lodash'
import { King } from 'common/BrinkmanKings'
import { MesopotamianDateDto } from 'fragmentarium/domain/FragmentDtos'
import { romanize } from 'romans'

export interface DateField {
value: string
isBroken?: boolean
isUncertain?: boolean
}

export interface MonthField extends DateField {
isIntercalary?: boolean
}

export enum Ur3Calendar {
ADAB = 'Adab',
GIRSU = 'Girsu',
IRISAGRIG = 'Irisagrig',
NIPPUR = 'Nippur',
PUZRISHDAGAN = 'Puzriš-Dagan',
UMMA = 'Umma',
UR = 'Ur',
}

export class MesopotamianDate {
year: DateField
month: MonthField
day: DateField
king?: King
isSeleucidEra?: boolean
ur3Calendar?: Ur3Calendar

constructor(
year: DateField,
month: MonthField,
day: DateField,
king?: King,
isSeleucidEra?: boolean,
ur3Calendar?: Ur3Calendar
) {
this.year = year
this.month = month
this.day = day
this.king = king
this.isSeleucidEra = isSeleucidEra
this.ur3Calendar = ur3Calendar
}

static fromJson(dateJSON: MesopotamianDateDto): MesopotamianDate {
const { year, month, day, king, isSeleucidEra, ur3Calendar } = dateJSON
return new MesopotamianDate(
year,
month,
day,
king,
isSeleucidEra,
ur3Calendar
)
}

toString(): string {
const dateParts = [
this.yearToString(),
this.monthToString(),
this.dayToString(),
]
return `${dateParts.join(
'.'
)}${this.kingOrEraToString()}${this.ur3CalendarToString()}`
}

yearToString(): string {
return this.year.value
? this.brokenAndUncertainToString('year', this.year.value)
: '∅'
}

monthToString(): string {
const intercalary = this.month.isIntercalary ? '²' : ''
const month = Number(this.month.value)
? romanize(Number(this.month.value))
: this.month.value
return month
? this.brokenAndUncertainToString('month', month + intercalary)
: '∅'
}

dayToString(): string {
return this.day.value
? this.brokenAndUncertainToString('day', this.day.value)
: '∅'
}

brokenAndUncertainToString(
parameter: 'year' | 'day' | 'month',
element: string
): string {
const { isBroken, isUncertain } = this[parameter]

return `${isBroken ? '⸢' : ''}${element}${isBroken ? '⸣' : ''}${
isUncertain ? '?' : ''
}`
}

kingOrEraToString(): string {
console.log('!!! isSeleucidEra', this.isSeleucidEra)
const eraOrKing = this.isSeleucidEra ? 'SE' : this.king?.name ?? ''
return eraOrKing ? ' ' + eraOrKing : eraOrKing
}

ur3CalendarToString(): string {
return this.ur3Calendar ? `, ${this.ur3Calendar} calendar` : ''
}

toGregorian(): string {
// ToDo: WiP, implement
return ''
}
}

/*
export class Dates {
readonly dates: ReadonlyArray<Date>

constructor(dates: Date[]) {
this.dates = dates
}

static fromJson(datesJSON: readonly MesopotamianDateDto[]): Dates {
return new Dates(datesJSON.map((dateJson) => Date.fromJson(dateJson)))
}


isPresent(date: Date): boolean {
return this.dates.some(
(element) =>
JSON.stringify(element.category) === JSON.stringify(date.category)
)
}

find(date: Date): Date | undefined {
return _.find(
this.dates,
(elem) => JSON.stringify(elem.category) === JSON.stringify(date.category)
)
}

insertWithOrder(date: Date, indexLookup: readonly string[][]): Dates {
return produce(this, (draft: Draft<Dates>) => {
draft.dates = castDraft(
_.sortBy([...this.dates, date], (date) =>
JSON.stringify(indexLookup).indexOf(JSON.stringify(date.category))
)
)
})
}

delete(date: Date): Dates {
return produce(this, (draft: Draft<Dates>) => {
draft.dates = castDraft(
this.dates.filter(
(elem) => JSON.stringify(elem) !== JSON.stringify(date)
)
)
})
}

replace(date: Date): Dates {
return produce(this, (draft: Draft<Dates>) => {
const dates = _.cloneDeep(this.dates as Date[])
const index = _.findIndex(
this.dates,
(content) =>
JSON.stringify(content.category) === JSON.stringify(date.category)
)
dates.splice(index, 1, date)
draft.dates = castDraft(dates)
})
}
*/
74 changes: 74 additions & 0 deletions src/fragmentarium/domain/DateConverter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import DateConverter from './DateConverter'

describe('DateConverter', () => {
let babylonDate: DateConverter

beforeEach(() => {
babylonDate = new DateConverter()
})

test('Set modern date', () => {
babylonDate.setModernDate(-310, 3, 3)
const expected = {
year: -310,
month: 3,
day: 3,
bcYear: '311',
julianDay: 1607892,
weekDay: 1,
babylonianDay: 29,
babylonianMonth: 11,
babylonianRuler: '4 Alexander IV Aegus',
seBabylonianYear: '0',
seMacedonianYear: '1',
arsacidYear: ' ',
babylonianLunation: 5393,
babylonianMonthLength: 29,
}
expect(babylonDate.calendar).toEqual(expected)
})

test('Set Babylonian date', () => {
// WiP.
// ToDo: Update to properly test.
babylonDate.setBabylonianDate(-200, 10, 1)
const expected = {
year: -625,
month: 11,
day: 27,
bcYear: '626',
julianDay: 1493107,
weekDay: 2,
babylonianDay: 1,
babylonianMonth: 9,
babylonianRuler: '1 Interregnum',
seBabylonianYear: '-314',
seMacedonianYear: ' ',
arsacidYear: ' ',
babylonianLunation: 1507,
babylonianMonthLength: 30,
}
expect(babylonDate.calendar).toEqual(expected)
})

test('Update year', () => {
babylonDate.updateYear(100)
expect(babylonDate.calendar.year).toBe(-210)
})

test('Update month', () => {
babylonDate.updateMonth(5)
expect(babylonDate.calendar.month).toBe(8)
expect(babylonDate.calendar.year).toBe(-310)
})

test('Update day', () => {
babylonDate.updateDay(10)
expect(babylonDate.calendar.day).toBe(13)
})

test('Get current day', () => {
babylonDate.calendar.day = 25
expect(babylonDate.getCurrentDay()).toBe(25)
})
})
Loading
Loading