-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.js
62 lines (54 loc) · 1.79 KB
/
printer.js
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
'use strict';
const { bullet, pointerSmall } = require('figures');
const { Level, Type } = require('./parser');
const chalk = require('chalk');
const Table = require('cli-table');
const formatter = {
[Level.High]: chalk.red.bold,
[Level.Moderate]: chalk.yellow.bold,
[Level.Low]: chalk.green.bold
};
const columns = (col, def) => col.length > 0 ? col : def;
const formatDate = date => {
const [, m, d] = date.split('-');
return `${d}.${m}.`;
};
const times = (n, cb) => Array.from({ length: n }, cb);
module.exports = function print({ type, data }) {
if (type === Type.Combined) return printCombined(data);
data.forEach(entry => printTable(entry));
};
function printCombined({ tree, grass, weed }) {
printTable(tree);
printPrevalent(tree);
printTable(grass);
printPrevalent(grass);
printTable(weed);
printPrevalent(weed);
}
function printTable({ label, name, records }) {
const title = label || name;
const head = [chalk.bold.blue(title), ...times(Math.max(records.length, 1), () => '')];
const dates = records.map(it => formatDate(it.date));
const values = records.map(it => formatLevel(it.level, it.value));
const table = new Table({ head });
table.push(
{ [chalk.reset('Datum')]: columns(dates, '') },
{ [chalk.reset('Koncentracija')]: columns(values, chalk.magenta('nema podataka')) }
);
console.log(table.toString());
}
function printPrevalent({ prevalent }) {
if (prevalent.length <= 0) return console.log();
console.log('', chalk.underline('Prevladava pelud:'));
prevalent.forEach(({ name }) => console.log('', pointerSmall, name));
console.log();
}
function formatLevel(level, value) {
let str = bullet + ' ';
str += level.label;
if (value) str += ` (${value})`;
const format = formatter[level.value];
if (format) return format(str);
return str;
}