aboutsummaryrefslogtreecommitdiff
path: root/src/tol.ts
blob: 42605e54f7183994384b7394c8fab8c2a67c6609 (plain)
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
/*
 * Provides classes for representing and working with tree-of-life data.
 */
 
// Represents a tree-of-life node/tree
export class TolNode {
	name: string;
	children: TolNode[];
	parent: TolNode | null;
	constructor(name: string, children: TolNode[] = [], parent = null){
		this.name = name;
		this.children = children;
		this.parent = parent;
	}
}
// Represents a tree-of-life node obtained from tolData.json
export class TolNodeRaw {
	name: string;
	children?: TolNodeRaw[];
	constructor(name: string, children: TolNodeRaw[] = []){
		this.name = name;
		this.children = children;
	}
}
// Converts a TolNodeRaw tree to a TolNode tree
export function tolFromRaw(node: TolNodeRaw): TolNode {
	function helper(node: TolNodeRaw, parent: TolNode | null){
		let tolNode = new TolNode(node.name);
		if (node.children == null){
			tolNode.children = [];
		} else {
			tolNode.children = node.children.map(child => helper(child, tolNode));
		}
		tolNode.parent = parent;
		return tolNode;
	}
	return helper(node, null);
}
// Returns a map from TolNode names to TolNodes in a given tree
export function getTolMap(tolTree: TolNode): Map<string, TolNode> {
	function helper(node: TolNode, map: Map<string, TolNode>){
		map.set(node.name, node);
		node.children.forEach(child => helper(child, map));
	}
	let map = new Map();
	helper(tolTree, map);
	return map;
}