-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrux-output.js
132 lines (109 loc) · 2.87 KB
/
crux-output.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
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
import { green, red, yellow, bold } from "./utils/format.js";
import { table } from "table";
function drawDistribution(histogram) {
function colorize(index, value) {
if (index === 0) {
return green(value);
}
if (index === 1) {
return yellow(value);
}
if (index === 2) {
return red(value);
}
}
function draw(value) {
let line = "";
for (let i = 0; i < value; i++) {
line += "\u25AA";
}
return line;
}
const distributionLine = histogram
.map((value, index) => {
return colorize(index, draw(Math.round(value)));
})
.join("")
.padEnd(128, "\u25AA");
const distributionValue = histogram
.map((value, index) => colorize(index, (value + "").padStart(5)))
.join(" ");
return distributionLine + " " + distributionValue + "\n";
}
function colorizeValue(value, rank, pad = 0) {
const v = (value + "").padEnd(pad);
if (rank === "poor") {
return red(v);
} else if (rank === "average") {
return yellow(v);
} else {
return green(v);
}
}
function printTable(data) {
const headings = Object.keys(data[0]).filter(
(item) => item !== "minimalGood",
);
const values = data.map((item) =>
Object.entries(item)
.filter((entry) => entry[0] !== "minimalGood")
.map((entry) => {
const it = entry[1];
return typeof it === "object" ? colorizeValue(it.p75, it.rank) : it;
}),
);
const tableData = [headings].concat(values);
const output = table(tableData);
console.log(output);
}
function printDistribution(data) {
const headings = Object.keys(data[0]);
const values = data
.map((item) =>
Object.entries(item)
.map((entry, i) => {
const [key, it] = entry;
if (key === "minimalGood") return;
if (typeof it === "object") {
return (
bold(headings[i].padStart(4)) +
"" +
colorizeValue(it.p75, it.rank, 6) +
drawDistribution(it.histogram)
);
} else {
return "\n" + bold(it) + "\n";
}
})
.join(""),
)
.join("");
console.log(values);
}
function printCSV(data) {
const titleRow = ["url", "metric", "p75", "good", "average", "poor"].join(
";",
);
const headings = Object.keys(data[0]);
const values = data
.map((item) => {
const url = item.url;
return Object.values(item)
.map((it, i) => {
if (typeof it === "object") {
return [url, headings[i], it.p75, it.histogram.join(";")].join(";");
}
})
.join("\n");
})
.join("\n");
console.log(titleRow + values);
}
//output
function printHeading(params) {
const heading = `\u001b[32m\nChrome UX Report \u001b[0m ${JSON.stringify(
params,
)}`;
console.log(heading);
}
export { printTable, printDistribution, printCSV, printHeading };