-
Notifications
You must be signed in to change notification settings - Fork 5
/
Cast.gs
63 lines (53 loc) · 1.74 KB
/
Cast.gs
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
function castTable(data, measure, value, def) {
var output = {};
var keys = Object.keys(data);
var valCols = [];
var idCols = keys.slice();
idCols.splice(idCols.indexOf(measure), 1);
idCols.splice(idCols.indexOf(value), 1);
function findFreeRow(row) {
if (idCols.length === 0) {return output[currentMeasure].length;}
for (var oRow = 0; oRow < nRow(output); oRow++) {
for (var idCol in idCols) {
var currentId = idCols[idCol];
if (output[currentId][oRow] !== data[currentId][row]) {
break;
}
}
if (idCol == idCols.length -1 &&
(output[currentMeasure][oRow] === def ||
output[currentMeasure][oRow] === undefined)) {
return oRow;
}
}
return -1;
}
// Create ID Columns
idCols.map(function(col) {output[col] = [];});
for (var row in data[measure]) {
var currentMeasure = data[measure][row];
var currentValue = data[value][row]
var outputKeys = Object.keys(output);
// Add new column if need be
if (outputKeys.indexOf(String(currentMeasure)) === -1) {
output[currentMeasure] = [];
outputKeys = Object.keys(output);
valCols.push(currentMeasure)
}
// Try to add data to an existing row or append a new row.
var freeRow = findFreeRow(row)
if (freeRow == -1) {
idCols.map(function(idCol) {output[idCol].push(data[idCol][row]);})
freeRow = nRow(output) - 1;
}
while (output[currentMeasure].length < freeRow) {
output[currentMeasure].push(def);
}
output[currentMeasure][freeRow] = currentValue;
}
// Fill columns up with default values
valCols.map(function(col) {
while (output[col].length < nRow(output)) {output[col].push(def);}
});
return output;
}