-
Notifications
You must be signed in to change notification settings - Fork 31
/
unist-compact.ts
89 lines (87 loc) · 2.5 KB
/
unist-compact.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Parent, Node } from "unist";
import visit from "unist-util-visit-parents";
import remove from "unist-util-remove";
function addValue(data: Node["data"], path: string, value: any) {
const existingValue = data![path];
if (!existingValue) {
data![path] = value;
} else {
data!["+" + path] = ((data!["+" + path] as any[]) || []).concat(value);
}
}
/**
* This applies an opinionated transformation to GEDCOM data,
* making it easier for common use cases. In the raw GEDCOM
* AST, attributes like birth years are represented as nodes.
* This transformation compresses those attributes into properties
* of a node’s `.data` member.
*
* Here's how this transformation works:
*
* For example, let's say you have this content:
*
* ```
* 0 INDI
* 1 BIRT
* 2 DATE 12 MAY 1920
* 1 DEAT
* 2 DATE 1960
* ```
*
* The output of `parse` will create nodes for the INDI, BIRT, DATE,
* DEAT, and DATE objects. If you simply want to ask 'when was this individual
* alive?' This can be a difficult question to answer. Compact will transform
* those nodes into a simplified form:
*
* ```js
* {
* type: "INDI",
* data: {
* formal_name: "INDIVIDUAL",
* "BIRTH/DATE": "12 MAY 1920",
* "DEATH/DATE": "1960",
* },
* value: undefined,
* children: [],
* }
* ```
*
* If there are multiple values for something like a birth date, they'll be
* included in an additional property with a `+`:
*
* {
* "BIRTH/DATE": "12 MAY 1920",
* "+BIRTH/DATE": ["13 MAY 1920"],
* }
*
* This also removes nodes from the syntax tree that are unlikely
* to have any use for genealogical or visualization applications.
*
* @param root - a parsed GEDCOM document
* @param removeNodes - a list of nodes that should be removed.
* @returns the same document, with attributes compacted.
*/
export function compact(
root: Parent,
removeNodes: string[] = ["TRLR", "SUBM", "SUBN", "HEAD", "NOTE", "SOUR"]
): Parent {
// Remove "trailer" objects, which are not useful to us.
remove(root, removeNodes);
for (let child of root.children) {
if (!child.data) child.data = {};
visit(child, (node, ancestors) => {
const path = ancestors
.slice(1)
.concat(node)
.map((a) => a.data?.formal_name || a.type)
.join("/");
if (node.value) {
addValue(child.data!, path, node.value);
} else if (node.data?.pointer) {
addValue(child.data!, `@${path}`, node.data.pointer);
}
});
child.children = [];
}
return root;
}