-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-parser.js
More file actions
61 lines (51 loc) · 1.81 KB
/
Copy pathdebug-parser.js
File metadata and controls
61 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
import fs from 'fs';
function parseGedcomFile(content) {
const lines = content.split('\n').map(line => line.replace(/\r$/, ''));
const records = [];
let stack = [];
for (const line of lines) {
if (!line.trim()) continue;
const match = line.match(/^(\d+)\s+(@[^@]+@\s+)?(\w+)(?:\s+(.*))?$/);
if (!match) continue;
const level = parseInt(match[1]);
const pointer = match[2]?.slice(0, -1) || null;
const tag = match[3];
const value = match[4] || '';
const record = { level, pointer, tag, value, tree: [] };
if (level === 0) {
records.push(record);
stack = [[0, record]];
} else {
while (stack.length > 0 && stack[stack.length - 1][0] >= level) {
stack.pop();
}
if (stack.length > 0) {
const parent = stack[stack.length - 1][1];
parent.tree.push(record);
}
stack.push([level, record]);
}
}
return records;
}
const inputPath = process.argv[2] || 'data/raw/example.ged';
if (!fs.existsSync(inputPath)) {
console.error(`File not found: ${inputPath}`);
console.error('Usage: node debug-parser.js data/raw/yourfile.ged');
process.exit(1);
}
const content = fs.readFileSync(inputPath, 'utf-8');
const records = parseGedcomFile(content);
const indiRecords = records.filter(r => r.tag === 'INDI').slice(0, 2);
indiRecords.forEach(indi => {
console.log('\nINDI:', indi.pointer);
console.log(' Tree length:', indi.tree.length);
const nameRecord = indi.tree.find(r => r.tag === 'NAME');
console.log(' NAME record:', nameRecord ? nameRecord.value : 'NOT FOUND');
const birtRecord = indi.tree.find(r => r.tag === 'BIRT');
if (birtRecord) {
const dateRecord = birtRecord.tree.find(r => r.tag === 'DATE');
console.log(' BIRT DATE:', dateRecord ? dateRecord.value : 'NOT FOUND');
}
});