-
Notifications
You must be signed in to change notification settings - Fork 313
Expand file tree
/
Copy pathdataConverter.js
More file actions
114 lines (104 loc) · 2.79 KB
/
dataConverter.js
File metadata and controls
114 lines (104 loc) · 2.79 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
var convertToJSON = function(data, origin) {
return (origin == 'cloc') ? convertFromClocToJSON(data) : convertFromWcToJSON(data);
};
/**
* Convert the output of cloc in csv to JSON format
*
* > cloc . --csv --exclude-dir=vendor,tmp --by-file --report-file=data.cloc
*/
var convertFromClocToJSON = function(data) {
var lines = data.split("\n");
lines.shift(); // drop the header line
var json = {};
lines.forEach(function(line) {
var cols = line.split(',');
var filename = cols[1];
if (!filename) return;
var elements = filename.split(/[\/\\]/);
var current = json;
elements.forEach(function(element) {
if (!current[element]) {
current[element] = {};
}
current = current[element];
});
current.language = cols[0];
current.size = parseInt(cols[4], 10);
});
json = getChildren(json)[0];
json.name = 'root';
return json;
};
/**
* Convert the output of wc to JSON format
*
* > git ls-files | xargs wc -l
*/
var convertFromWcToJSON = function(data) {
var lines = data.split("\n");
var json = {};
var filename, size, cols, elements, current;
lines.forEach(function(line) {
cols = line.trim().split(' ');
size = parseInt(cols[0], 10);
if (!size) return;
filename = cols[1];
if (filename === "total") return;
if (!filename) return;
elements = filename.split(/[\/\\]/);
current = json;
elements.forEach(function(element) {
if (!current[element]) {
current[element] = {};
}
current = current[element];
});
current.size = size;
});
json.children = getChildren(json);
json.name = 'root';
return json;
};
/**
* Convert a simple json object into another specifying children as an array
* Works recursively
*
* example input:
* { a: { b: { c: { size: 12 }, d: { size: 34 } }, e: { size: 56 } } }
* example output
* { name: a, children: [
* { name: b, children: [
* { name: c, size: 12 },
* { name: d, size: 34 }
* ] },
* { name: e, size: 56 }
* ] } }
*/
var getChildren = function(json) {
var children = [];
if (json.isleaf) return children;
for (var key in json) {
var child = { name: key };
if (json[key].size) {
// value node
child.size = json[key].size;
child.language = json[key].language;
child.isleaf = true;
} else {
// children node
var childChildren = getChildren(json[key]);
if (childChildren) child.children = childChildren;
}
children.push(child);
delete json[key];
}
return children;
};
// Recursively count all elements in a tree
var countElements = function(node) {
var nbElements = 1;
if (node.children) {
nbElements += node.children.reduce(function(p, v) { return p + countElements(v); }, 0);
}
return nbElements;
};