-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathprintSystems.js
More file actions
69 lines (60 loc) · 1.73 KB
/
printSystems.js
File metadata and controls
69 lines (60 loc) · 1.73 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
62
63
64
65
66
67
68
69
'use strict';
/**
* A tool for printing out the systems in a tree form.
*/
import { readFile } from 'fs/promises';
const json = JSON.parse(await readFile(new URL('./systems.json', import.meta.url)));
const treeSystems = { descendents: [] };
unflatten();
const count = printTree(treeSystems, 0);
console.log('System Count: ' + count);
function unflatten() {
var processedNode = false;
for (const [systemKey, system] of Object.entries(json.systems)) {
if (!system.processed) {
if (system.inherits) {
processedNode = searchDescendents(treeSystems, system, systemKey);
} else {
addDescendent(treeSystems, system, systemKey);
system.processed = true;
processedNode = true;
}
}
}
if (processedNode) {
unflatten();
}
}
function searchDescendents(curElement, system, systemKey) {
var processedNode = false;
var foundElement = curElement.descendents.find(descendent => descendent.key === system.inherits);
if (foundElement) {
addDescendent(foundElement, system, systemKey);
system.processed = true;
processedNode = true;
} else {
curElement.descendents.forEach(descendent => {
if (!processedNode) {
processedNode = searchDescendents(descendent, system, systemKey);
}
});
}
return processedNode;
}
function addDescendent(element, system, systemKey) {
element.descendents.push({
name: system.name,
key: systemKey,
historical: system.historical,
descendents: []
});
}
function printTree(tree, indent) {
var count = 0;
tree.descendents.forEach(descendent => {
var historical = descendent.historical ? ' *(H)*' : '';
console.log(' '.repeat(indent + 1) + '- ' + descendent.key + ' (' + descendent.name + ')' + historical);
count += 1 + printTree(descendent, indent + 1);
});
return count;
}