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

Convert mappings to TS instead of JSON #103

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading