UNPKG

gedcom-ts

Version:

A TypeScript library to create genealogy tree data model and to import/export data in a GEDCOM file (.ged).

93 lines (92 loc) 3.27 kB
import { People, Sex } from '../commons/People'; export class ReadGed { gedcom; peoples; partnersMap; childsMap; constructor(gedcom) { this.gedcom = gedcom; this.peoples = this.import(); this.partnersMap = new Map(); this.childsMap = new Map(); this.groupPartners(); } get informationHeadFile() { if (this.separate) { return this.separate.find((information) => information.startsWith('0 HEAD')); } return null; } get informationPeoplesFile() { if (this.separate) { return this.separate.filter((information) => information.startsWith('0 @I')); } return null; } get informationMarriagesFile() { if (this.separate) { return this.separate.filter((information) => information.startsWith('0 @F')); } return null; } get separate() { const splitGedcom = this.gedcom.split('\n0 '); return splitGedcom.map((split) => split.startsWith('0') ? split : `0 ${split}`); } import() { return Create.json(this.informationPeoplesFile, this.informationMarriagesFile); } groupPartners() { [...this.peoples].forEach(people => { if (people.FAMS?.length > 0) { people.FAMS.forEach(oneFams => { let partners = [people]; if (this.partnersMap.has(oneFams)) { partners = [...partners, ...this.partnersMap.get(oneFams)]; } this.partnersMap.set(oneFams, partners); }); } if (people.FAMC) { let allChilds = [people]; if (this.childsMap.has(people.FAMC)) { allChilds = [...allChilds, ...this.childsMap.get(people.FAMC)]; } this.childsMap.set(people.FAMC, allChilds); } }); } createDirectAncestries(people) { return this.haveParentsOfPeople(people, []) .sort((a, b) => a.sosa - b.sosa); } haveParentsOfPeople(people, directAncestries, sosaChild) { const copyPeople = Object.assign({}, people); copyPeople.sosa = !sosaChild ? 1 : (sosaChild * 2) + (people.sex === Sex.F ? 1 : 0); if (this.partnersMap.has(copyPeople.FAMC)) { const parents = [...this.partnersMap.get(copyPeople.FAMC)]; if (parents && parents.length > 0) { parents.forEach(parent => { this.haveParentsOfPeople(parent, directAncestries, copyPeople.sosa); }); } } directAncestries.push(copyPeople); return directAncestries; } } export class Create { static json(peoplesFile, marriagesFile) { if (peoplesFile && peoplesFile.length > 0) { return peoplesFile .map((peopleFile) => { const peopleLines = peopleFile.split('\n'); const newPeopleJson = new People(); newPeopleJson.createPeopleJson(peopleLines); return newPeopleJson; }) .sort((n1, n2) => n1.FAMC - n2.FAMC); } return null; } }