Skip to content

Commit

Permalink
Redesign DateRange (#438)
Browse files Browse the repository at this point in the history
* prevent double trailing '.'

* add PartialDate; update and refactor archaeology dtos

* add test for PartialDate

* add missing fromDateRangeDto call

* remove unused DateRange

* rename CommentedDateRange -> DateRange

* omit findspot from update

* simplify toFindspotDto

* rename dateRange field
  • Loading branch information
fsimonjetz authored Feb 2, 2024
1 parent f8a86b6 commit 92648e2
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 169 deletions.
5 changes: 0 additions & 5 deletions src/chronology/domain/DateBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ export interface DateField {
isUncertain?: boolean
}

export interface DateRange {
start: Date
end: Date
}

export interface MonthField extends DateField {
isIntercalary?: boolean
}
Expand Down
8 changes: 3 additions & 5 deletions src/fragmentarium/application/FragmentService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ import { wordFactory } from 'test-support/word-fixtures'
import { silenceConsoleErrors } from 'setupTests'
import { QueryResult } from 'query/QueryResult'
import { MesopotamianDate } from 'chronology/domain/Date'
import {
Archaeology,
ArchaeologyDto,
toArchaeologyDto,
} from 'fragmentarium/domain/archaeology'
import { Archaeology } from 'fragmentarium/domain/archaeology'
import { ArchaeologyDto } from 'fragmentarium/domain/archaeologyDtos'
import { toArchaeologyDto } from 'fragmentarium/domain/archaeologyDtos'

jest.mock('./LemmatizationFactory')

Expand Down
2 changes: 1 addition & 1 deletion src/fragmentarium/application/FragmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { ManuscriptAttestation } from 'corpus/domain/manuscriptAttestation'
import { FragmentQuery } from 'query/FragmentQuery'
import { MesopotamianDate } from 'chronology/domain/Date'
import { FragmentAfoRegisterQueryResult, QueryResult } from 'query/QueryResult'
import { ArchaeologyDto } from 'fragmentarium/domain/archaeology'
import { ArchaeologyDto } from 'fragmentarium/domain/archaeologyDtos'

export const onError = (error) => {
if (error.message === '403 Forbidden') {
Expand Down
2 changes: 1 addition & 1 deletion src/fragmentarium/domain/FragmentDtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import MuseumNumber from './MuseumNumber'
import { King } from 'chronology/ui/BrinkmanKings'
import { Ur3Calendar } from 'chronology/domain/DateBase'
import { Eponym } from 'chronology/ui/Eponyms'
import { ArchaeologyDto } from './archaeology'
import { ArchaeologyDto } from './archaeologyDtos'

interface MeasureDto {
value?: number
Expand Down
27 changes: 14 additions & 13 deletions src/fragmentarium/domain/archaeology.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import {
import {
BuildingType,
Findspot,
FindspotDto,
PartialDate,
SiteKey,
createArchaeology,
excavationSites,
} from './archaeology'
import {
FindspotDto,
fromFindspotDto,
fromPlanDto,
toArchaeologyDto,
toFindspotDto,
toPlanDto,
} from './archaeology'
} from './archaeologyDtos'
import { createArchaeology, toArchaeologyDto } from './archaeologyDtos'
import MuseumNumber, { museumNumberToString } from './MuseumNumber'
import {
cslDataFactory,
Expand All @@ -42,10 +44,9 @@ const planDto = {
references: [referenceDto],
}
const plan = { svg: '<svg></svg>', references: [reference] }
const dateRange = dateRangeFactory.build()
const findspot = findspotFactory.build({
site: excavationSites[site],
dateRange: dateRange,
date: dateRangeFactory.build(),
plans: [plan],
})
const findspotDto: FindspotDto = {
Expand All @@ -59,7 +60,7 @@ const findspotDto: FindspotDto = {
'context',
'primaryContext',
'notes',
'dateRange'
'date'
),
_id: findspot.id,
site: site,
Expand All @@ -70,9 +71,9 @@ const displayParams: Partial<Findspot> = {
building: 'a house',
buildingType: 'RESIDENTIAL' as BuildingType,
levelLayerPhase: 'II',
dateRange: {
start: -1200,
end: -1150,
date: {
start: new PartialDate(-1200),
end: new PartialDate(-1150),
notes: '',
},
notes: '',
Expand Down Expand Up @@ -121,7 +122,7 @@ test('createArchaeology', () => {
test.each([
[
'with area and notes',
{ ...displayParams, area: 'some area', notes: 'general notes' },
{ ...displayParams, area: 'some area', notes: 'general notes.' },
'some area > a house (Residential), II (1200 BCE - 1150 BCE), general notes.',
],
[
Expand All @@ -141,14 +142,14 @@ test.each([
],
[
'no levelLayerPhase and date',
{ ...displayParams, levelLayerPhase: '', dateRange: null },
{ ...displayParams, levelLayerPhase: '', date: null },
'a house (Residential).',
],
[
'with date notes',
{
...displayParams,
dateRange: { ...displayParams.dateRange, notes: 'date notes' },
date: { ...displayParams.date, notes: 'date notes' },
},
'a house (Residential), II (1200 BCE - 1150 BCE, date notes).',
],
Expand Down
159 changes: 33 additions & 126 deletions src/fragmentarium/domain/archaeology.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Reference from 'bibliography/domain/Reference'
import MuseumNumber, { museumNumberToString } from './MuseumNumber'
import { Provenances } from 'corpus/domain/provenance'
import _ from 'lodash'
import { immerable } from 'immer'
import { ReferenceDto } from 'bibliography/domain/referenceDto'
import createReference from 'bibliography/application/createReference'

export const excavationSites = {
..._.omit(Provenances, 'Standard Text'),
Expand All @@ -29,41 +25,51 @@ export interface ExcavationPlan {
readonly svg: string
readonly references: readonly Reference[]
}
export type CommentedDateRange = {
start?: number
end?: number
notes?: string
}
export type CommentedDateRangeDto = {
start?: number
end?: number
notes?: string
}
export class PartialDate {
readonly year: number
readonly month?: number | null
readonly day?: number | null

constructor(
year: number,
month: number | null = null,
day: number | null = null
) {
this.year = year
this.month = month
this.day = day
}

function makeDate(date?: number) {
return date || date === 0 ? `${Math.abs(date)}${date < 0 ? ' BCE' : ''}` : ''
toString(): string {
return this.year >= 0
? _.reject([this.year, this.month, this.day], _.isNil).join('/')
: `${Math.abs(this.year)} BCE`
}
}
export type DateRange = {
start: PartialDate
end?: PartialDate | null
notes?: string | null
}
function pad(s?: string, left = ' ', right = ' '): string {
function pad(s?: string | number | null, left = ' ', right = ' '): string {
return s ? `${left}${s}${right}` : ''
}
function padLeft(s?: string, left = ' '): string {
function padLeft(s?: string | number | null, left = ' '): string {
return pad(s, left, '')
}
function padRight(s: string, right = ' '): string {
function padRight(s: string | number | null, right = ' '): string {
return pad(s, '', right)
}

export class Findspot {
readonly [immerable] = true

constructor(
readonly id: number,
readonly site: ExcavationSite = excavationSites[''],
readonly area: string = '',
readonly building: string = '',
readonly buildingType: BuildingType | null = null,
readonly levelLayerPhase: string = '',
readonly dateRange: CommentedDateRange | null = null,
readonly date: DateRange | null = null,
readonly plans: readonly ExcavationPlan[] = [],
readonly room: string = '',
readonly context: string = '',
Expand All @@ -72,9 +78,9 @@ export class Findspot {
) {}

private dateString(): string {
const start = makeDate(this.dateRange?.start)
const end = makeDate(this.dateRange?.end)
const notes = padLeft(this.dateRange?.notes, ', ')
const start = this.date?.start.toString()
const end = this.date?.end?.toString()
const notes = padLeft(this.date?.notes, ', ')

return end ? ` (${start} - ${end}${notes})` : start ? ` (${start})` : ''
}
Expand All @@ -92,114 +98,15 @@ export class Findspot {
const buildingSep = this.levelLayerPhase || dateInfo || notes ? ',' : ''
return `${area}${this.building}${buildingTypeInfo}${buildingSep}${padLeft(
this.levelLayerPhase
)}${dateInfo}${notes}.`
)}${dateInfo}${_.trimEnd(notes, '.')}.`
}
}

export interface Archaeology {
readonly excavationNumber?: string
readonly site?: ExcavationSite
readonly isRegularExcavation?: boolean
readonly excavationDate?: DateRange | null
readonly findspotId?: number | null
readonly findspot?: Findspot | null
}

interface PlanDto {
svg: string
references: readonly ReferenceDto[]
}

export type FindspotDto = Pick<
Findspot,
| 'area'
| 'building'
| 'buildingType'
| 'levelLayerPhase'
| 'room'
| 'context'
| 'primaryContext'
| 'notes'
> & {
_id: number
site: SiteKey
dateRange: CommentedDateRangeDto | null
plans: readonly PlanDto[]
}

export type ArchaeologyDto = Omit<Archaeology, 'site' | 'findspot'> & {
site?: SiteKey
findspot?: FindspotDto | null
}

export function fromPlanDto(dto: PlanDto): ExcavationPlan {
return {
svg: dto.svg,
references: dto.references.map(createReference),
}
}
export function toPlanDto(plan: ExcavationPlan): PlanDto {
return {
svg: plan.svg,
references: plan.references.map((reference) => ({
..._.pick(reference, 'id', 'type', 'pages', 'notes', 'linesCited'),
document: reference.document.toCslData(),
})),
}
}

export function fromFindspotDto(dto: FindspotDto): Findspot {
return new Findspot(
dto._id,
excavationSites[dto.site || ''],
dto.area,
dto.building,
dto.buildingType,
dto.levelLayerPhase,
dto.dateRange,
dto.plans.map(fromPlanDto),
dto.room,
dto.context,
dto.primaryContext,
dto.notes
)
}

export function toFindspotDto(findspot: Findspot): FindspotDto {
return {
_id: findspot.id,
area: findspot.area,
building: findspot.building,
buildingType: findspot.buildingType,
levelLayerPhase: findspot.levelLayerPhase,
room: findspot.room,
context: findspot.context,
primaryContext: findspot.primaryContext,
notes: findspot.notes,
site: findspot.site.name as SiteKey,
dateRange: findspot.dateRange,
plans: findspot.plans.map(toPlanDto),
}
}

export function createArchaeology(
dto: Omit<ArchaeologyDto, 'excavationNumber'> & {
excavationNumber?: MuseumNumber
}
): Archaeology {
return {
...dto,
excavationNumber: dto.excavationNumber
? museumNumberToString(dto.excavationNumber)
: undefined,
site: excavationSites[dto.site || ''],
findspot: dto.findspot ? fromFindspotDto(dto.findspot) : null,
}
}

export function toArchaeologyDto(archaeology: Archaeology): ArchaeologyDto {
return {
...archaeology,
site: (archaeology.site?.name || '') as SiteKey,
findspot: archaeology.findspot ? toFindspotDto(archaeology.findspot) : null,
}
}
Loading

0 comments on commit 92648e2

Please sign in to comment.