UNPKG

gedcom-ts

Version:

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

89 lines (88 loc) 3.71 kB
import { Act } from './Act'; import { Identificator } from "./Identificator.enum"; import { findLinesConcernedByKeyword, findValueInLine } from "./Utils-People"; import { DateAct } from "./DateAct"; export var Sex; (function (Sex) { Sex["M"] = "M"; Sex["F"] = "F"; })(Sex || (Sex = {})); export class People { sosa; sex; firstnames; lastname; birth; marriage; death; FAMC; FAMS; photo; constructor(sosa, sex, firstnames, lastname, birth, marriage, death, FAMC, FAMS, photo) { this.sosa = sosa; this.sex = sex; this.firstnames = firstnames; this.lastname = lastname; this.photo = photo; this.birth = birth; this.marriage = marriage; this.death = death; this.FAMC = FAMC; this.FAMS = FAMS; } createPeopleJson(peopleLines) { const peoplesLinesWithoutCarriage = peopleLines.map(people => people.replace(/[\n\r]+/g, '')); this.createFirstnamesAndLastnameJson(peoplesLinesWithoutCarriage); this.createSex(peoplesLinesWithoutCarriage); this.createFAM(peoplesLinesWithoutCarriage); this.createPhoto(peoplesLinesWithoutCarriage); this.birth = this.createBirthDeathAct(peoplesLinesWithoutCarriage, Identificator.BIRT); this.death = this.createBirthDeathAct(peoplesLinesWithoutCarriage, Identificator.DEAT); } createFirstnamesAndLastnameJson(peopleLines) { let lineName = peopleLines.find((information) => information ? information.startsWith('1 NAME') : false); lineName = lineName ? lineName.replace('1 NAME', '').trim() : null; const regex = /\/(.*?)\//; const execLastName = regex.exec(lineName); this.lastname = execLastName ? execLastName[1].trim().toUpperCase() : null; lineName = execLastName && execLastName[0] ? lineName.replace(execLastName[0], '').trim() : lineName; this.firstnames = lineName ? lineName.replace(new RegExp('"', 'g'), '').trim().split(' ') : null; } createSex(peopleLines) { this.sex = findValueInLine(peopleLines, '1 SEX ') === Sex.M ? Sex.M : Sex.F; } // Enfant issu de la Famille (union de 2 parents) createFAM(peopleLines) { const numberPattern = /\d+/g; const famc = findValueInLine(peopleLines, '1 FAMC '); if (famc) { this.FAMC = Number(famc.match(numberPattern)); } const fams = findLinesConcernedByKeyword(peopleLines, '1 FAMS ').map(line => Number(line.match(numberPattern))); if (fams?.length > 0) { this.FAMS = fams?.length > 0 ? fams : []; } } createPhoto(peopleLines) { const photoUrl = findValueInLine(peopleLines, '2 FILE '); if (photoUrl) { this.photo = photoUrl; } } createBirthDeathAct(peopleLines, identificator) { const idxBegin = peopleLines.findIndex((lineInformation) => lineInformation.startsWith(`1 ${identificator}`)); const startArray = peopleLines.slice(idxBegin + 1); const idxEnd = startArray.findIndex((lineInformation) => lineInformation.startsWith('1 ')); const finalArray = idxEnd >= 0 ? startArray.slice(0, idxEnd) : startArray; return new Act(new DateAct(findValueInLine(finalArray, '2 DATE ')), findValueInLine(finalArray, '2 PLAC ')); } } export class TreePeople extends People { parents; childrens; constructor(people, parents, childrens, currentSosa) { super(currentSosa, people.sex, people.firstnames, people.lastname, people.birth, people.marriage, people.death, people.FAMC, people.FAMS); this.parents = parents; this.childrens = childrens; } }