-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.js
More file actions
139 lines (114 loc) · 3.13 KB
/
Copy pathtree.js
File metadata and controls
139 lines (114 loc) · 3.13 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var Tree = function (value, id) {
this.value = value;
this.id = id;
this.children = [];
this.cached_sum = undefined;
};
Tree.prototype.addChild = function (child) {
this.children.push(child);
};
Tree.prototype.getChildren = function () {
return this.children;
};
// turn value into string with comma as decimal point
var commafy = function (value) {
var s = "" + value;
return s.replace(".", ",");
}
// turn comma based decimal string to period based
var uncommafy = function (s) {
return s.replace(",", ".");
}
// Since we recurse to get all values, the sum is calculated only once, then
// cached
Tree.prototype.sum = function () {
var total = this.value;
var l = this.children.length;
var i = 0;
if (this.cached_sum === undefined) {
for (; i<l; i++)
{
total += this.children[i].sum();
}
this.cached_sum = total;
}
return this.cached_sum;
};
var splitOnLineEndings = function (s) {
return s.replace(/\r\n|\r/g, '\n').split("\n");
};
var textTo2dArray = function (textTable) {
var lines = splitOnLineEndings(textTable);
var rows = [];
var row;
var isNumber = function (s) {
s = uncommafy(s);
return (s - 0) == s && s.length > 0;
};
var validLine = function(line) {
var id = line.split("\t")[2];
return line && isNumber(id);
};
for (var i=0, length=lines.length; i<length; i++) {
if (validLine(lines[i])) {
row = lines[i].split("\t");
rows.push(row);
}
}
return rows;
};
var Table = function (textTable) {
this.rows = textTo2dArray(textTable);
this.length = this.rows.length;
};
Table.prototype.eachLine = function (f) {
var line, parent, id, value, i;
for (i=0; i<this.length; i++) {
line = this.rows[i];
parent = line[0];
id = line[1];
value = line[2];
f(parent, id, value);
}
};
Table.prototype.makeNodes = function () {
var nodes = {};
var firstLine = true;
var addNode = function (parent, id, value) {
if (value) {
value = uncommafy(value);
}
nodes[id] = new Tree(parseFloat(value, 10), id);
};
var addToParent = function (parent, id, value) {
if (firstLine) {
firstLine = false;
if (isNaN(parseInt(value, 10))) {
return;
}
}
if (parent !== "") {
nodes[parent].addChild(nodes[id]);
}
};
this.eachLine(addNode);
this.eachLine(addToParent);
return nodes;
};
// main function, add row of sums to table of nodes
var addSumsToTable = function (tableText) {
var table = new Table(tableText);
var nodes = table.makeNodes();
var out = [];
table.eachLine(function(parent, id, value) {
var sum = commafy(nodes[id].sum());
out.push([parent, id, value, sum].join("\t"));
});
return { output: out.join("\n") };
};
// export to node.js module
if(typeof(exports) !== 'undefined' && exports !== null) {
exports.Tree = Tree;
exports.Table = Table;
exports.addSumsToTable = addSumsToTable;
}