-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbarChart.js
More file actions
39 lines (29 loc) · 940 Bytes
/
barChart.js
File metadata and controls
39 lines (29 loc) · 940 Bytes
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
const { join } = require('path');
const { readFileSync } = require('fs');
const max = arr =>
Math.max(...arr);
const maxLen = strings =>
max(strings.map(str => str.length));
const rightPad = (input, n) =>
input + " ".repeat(n);
const graph = (renderMax, data) => {
const labels = Object.keys(data)
.map((label, i, labels) => rightPad(label, maxLen(labels) - label.length))
const values = Object.values(data)
.map((datum, i, allDatums) => (datum / max(allDatums)) * renderMax)
let toReturn = '```\n';
labels.forEach((label, i) => {
toReturn += label + ' ' + '░'.repeat(values[i]) + '\n';
})
return toReturn + '\n```\n';
}
exports.generateGraph = (dir, files) => {
const data = files
.reduce((obj, filePath) => {
try {
obj[filePath.replace(dir, '')] = readFileSync(filePath, 'utf8').split('\n').length;
} catch(e) {}
return obj;
}, {});
return graph(28, data);
}