UNPKG

gedcom-ts

Version:

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

131 lines (130 loc) 4.26 kB
export var TypeDateAct; (function (TypeDateAct) { TypeDateAct["BEF"] = "BEF"; TypeDateAct["ABT"] = "ABT"; TypeDateAct["NULL"] = "NULL"; })(TypeDateAct || (TypeDateAct = {})); export class DateAct { typeDateAct; day; month; year; date; constructor(dateLine) { if (dateLine && dateLine !== 'inconnue') { const dateParts = dateLine.split(' '); switch (dateParts.length) { case 1: if (dateParts[0].includes('/')) { const [month, year] = dateParts[0].split('/'); this.month = this.convertMonth(month); this.year = year; } else { this.year = dateParts[0]; } break; case 2: if (dateParts[0] === TypeDateAct.BEF || dateParts[0] === TypeDateAct.ABT) { this.typeDateAct = dateParts[0]; if (dateParts[1].includes('/')) { const [month, year] = dateParts[1].split('/'); this.month = this.convertMonth(month); this.year = year; } else { this.month = ''; this.year = dateParts[1]; } } else if (dateParts[0].includes('/')) { const [month, year] = dateParts[0].split('/'); this.month = this.convertMonth(month); this.year = year; } else { this.month = dateParts[0]; this.year = dateParts[1]; } break; case 3: if (dateParts[0] === TypeDateAct.BEF || dateParts[0] === TypeDateAct.ABT) { this.typeDateAct = dateParts[0]; this.month = dateParts[1]; this.year = dateParts[2]; } else if (dateParts[1].includes('/')) { const [month, year] = dateParts[1].split('/'); this.month = this.convertMonth(month); this.year = year; } else { this.day = dateParts[0]; this.month = dateParts[1]; this.year = dateParts[2]; } break; } this.date = this.parseDate(); } } convertMonth(month) { const monthMap = { '1': 'JAN', '2': 'FEB', '3': 'MAR', '4': 'APR', '5': 'MAY', '6': 'JUN', '7': 'JUL', '8': 'AUG', '9': 'SEP', '10': 'OCT', '11': 'NOV', '12': 'DEC', }; if (monthMap[month]) { return monthMap[month]; } return month.toUpperCase(); } parseDate() { const months = { JAN: 0, FEB: 1, MAR: 2, APR: 3, MAY: 4, JUN: 5, JUL: 6, AUG: 7, SEP: 8, OCT: 9, NOV: 10, DEC: 11, }; if (!this.year) { return null; // Année manquante, la date ne peut pas être analysée } let day = 1; if (this.day) { day = parseInt(this.day, 10); if (isNaN(day)) { return null; // Jour invalide } } let month = 0; if (this.month) { const monthKey = this.month.toUpperCase(); month = months[monthKey]; if (month === undefined) { return null; // Mois invalide } } let year = parseInt(this.year, 10); if (isNaN(year)) { return null; // Année invalide } return new Date(year, month, day); } }