Skip to content

Commit

Permalink
Merge pull request #103 from riccardofano/mappings-to-ts
Browse files Browse the repository at this point in the history
Convert mappings to TS instead of JSON
  • Loading branch information
riccardofano authored Feb 22, 2024
2 parents 0ae1608 + 5f0e60f commit 84b7589
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 42 deletions.
21 changes: 5 additions & 16 deletions core/invertGraph.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CategoryName } from './types/Category'
import { Person, PersonList } from './types/Person'
import mappings from './invertedMappings.json'
import { Mapping, MAPPINGS } from './invertedMappings'

export type InvertedPerson = {
id: string
Expand All @@ -26,8 +25,7 @@ export function invertGraph(root: Person, list: InvertedPerson[]): PersonList {
const personList: PersonList = { '0': root }

for (const person of list) {
const degree = relationIdToDegree(person.relation)
const category = relationIdToCategory(person.relation)
const { category, degree } = relationIdToMapping(person.relation)
const root = person.relatedTo === '' ? '0' : person.relatedTo

personList[person.id] = {
Expand All @@ -52,20 +50,11 @@ export function invertGraph(root: Person, list: InvertedPerson[]): PersonList {
return personList
}

function relationIdToDegree(relation: string): number {
const mapping = mappings[relation as keyof typeof mappings]
function relationIdToMapping(relation: string): Mapping {
const mapping = MAPPINGS[relation]
if (!mapping) {
throw new Error('Unknown relation')
}

return mapping.degree
}

function relationIdToCategory(relation: string): CategoryName {
const mapping = mappings[relation as keyof typeof mappings]
if (!mapping) {
throw new Error('Unknown relation')
}

return mapping.category as CategoryName
return mapping
}
22 changes: 0 additions & 22 deletions core/invertedMappings.json

This file was deleted.

26 changes: 26 additions & 0 deletions core/invertedMappings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CategoryName } from './types/Category'

export type Mapping = { category: CategoryName; degree: number }

export const MAPPINGS: Record<string, Mapping> = {
figlio: { category: 'children', degree: 1 },
'nipote in linea retta': { category: 'children', degree: 2 },
'pronipote in linea retta': { category: 'children', degree: 3 },
'abnipote in linea retta': { category: 'children', degree: 4 },
genitore: { category: 'ascendants', degree: 1 },
fratello: { category: 'bilateral', degree: 2 },
'nipote in linea collaterale': { category: 'children', degree: 3 },
'pronipote in linea collaterale': { category: 'children', degree: 4 },
'abnipote in linea collaterale': { category: 'children', degree: 5 },
zio: { category: 'others', degree: 3 },
cugino: { category: 'children', degree: 4 },
'figlio di cugino': { category: 'children', degree: 5 },
'nipote di cugino': { category: 'children', degree: 6 },
nonno: { category: 'ascendants', degree: 2 },
prozio: { category: 'others', degree: 4 },
'secondo cugino': { category: 'children', degree: 5 },
'figlio di secondo cugino': { category: 'children', degree: 6 },
bisavo: { category: 'ascendants', degree: 3 },
trisavo: { category: 'children', degree: 4 },
coniuge: { category: 'spouse', degree: 1 },
}
8 changes: 4 additions & 4 deletions core/tests/invertedGraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ test('siblings', () => {

// prettier-ignore
expect(graph).toStrictEqual({
'1': { id: list[0].id, name: list[0].name, available: list[0].available, degree: 2, category: 'bilinear', relatives: [], previous: '0' },
'2': { id: list[1].id, name: list[1].name, available: list[1].available, degree: 2, category: 'bilinear', relatives: [], previous: '0' },
'3': { id: list[2].id, name: list[2].name, available: list[2].available, degree: 2, category: 'bilinear', relatives: [], previous: '0' },
'4': { id: list[3].id, name: list[3].name, available: list[3].available, degree: 2, category: 'bilinear', relatives: [], previous: '0' },
'1': { id: list[0].id, name: list[0].name, available: list[0].available, degree: 2, category: 'bilateral', relatives: [], previous: '0' },
'2': { id: list[1].id, name: list[1].name, available: list[1].available, degree: 2, category: 'bilateral', relatives: [], previous: '0' },
'3': { id: list[2].id, name: list[2].name, available: list[2].available, degree: 2, category: 'bilateral', relatives: [], previous: '0' },
'4': { id: list[3].id, name: list[3].name, available: list[3].available, degree: 2, category: 'bilateral', relatives: [], previous: '0' },
'0': { id: '0', name: 'Defunto', available: false, degree: 0, category: 'root', relatives: ['1', '2', '3', '4'], previous: null },
})
})
Expand Down

0 comments on commit 84b7589

Please sign in to comment.