diff --git a/core/invertGraph.ts b/core/invertGraph.ts index 975a60c..5e9c223 100644 --- a/core/invertGraph.ts +++ b/core/invertGraph.ts @@ -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 @@ -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] = { @@ -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 } diff --git a/core/invertedMappings.json b/core/invertedMappings.json deleted file mode 100644 index 695ec15..0000000 --- a/core/invertedMappings.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "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": "bilinear", "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 } -} diff --git a/core/invertedMappings.ts b/core/invertedMappings.ts new file mode 100644 index 0000000..6800d95 --- /dev/null +++ b/core/invertedMappings.ts @@ -0,0 +1,26 @@ +import { CategoryName } from './types/Category' + +export type Mapping = { category: CategoryName; degree: number } + +export const MAPPINGS: Record = { + 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 }, +} diff --git a/core/tests/invertedGraph.test.ts b/core/tests/invertedGraph.test.ts index c855da9..feab8bc 100644 --- a/core/tests/invertedGraph.test.ts +++ b/core/tests/invertedGraph.test.ts @@ -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 }, }) })