| 1 | 'use strict'; |
| 2 | |
| 3 | function printGrid(colHeaders, rows, getValue, unit, note) { |
| 4 | const labelWidth = Math.max( |
| 5 | ...rows.map(function (r) { |
| 6 | return r[0].length; |
| 7 | }) |
| 8 | ); |
| 9 | const suffix = unit ? ' ' + unit : ''; |
| 10 | const fmtVal = function (v) { |
| 11 | return (v.toFixed(1) + suffix).padStart(10 + suffix.length); |
| 12 | }; |
| 13 | const fmtPct = function (v) { |
| 14 | return ((v >= 0 ? '+' : '') + v.toFixed(1) + '%').padStart(8); |
| 15 | }; |
| 16 | const fmtFactor = function (va, vb) { |
| 17 | return ((vb / va).toFixed(2) + 'x').padStart(7); |
| 18 | }; |
| 19 | const colWidth = 10 + suffix.length; |
| 20 | |
| 21 | const header = |
| 22 | ''.padEnd(labelWidth) + |
| 23 | ' ' + |
| 24 | colHeaders |
| 25 | .map(function (h) { |
| 26 | return h.padStart(colWidth); |
| 27 | }) |
| 28 | .join(' ') + |
| 29 | ' Delta Factor'; |
| 30 | console.log(' ' + header); |
| 31 | console.log(' ' + '-'.repeat(header.length)); |
| 32 | for (const [label, a, b] of rows) { |
| 33 | const va = getValue(a); |
| 34 | const vb = getValue(b); |
| 35 | const pct = ((vb - va) / va) * 100; |
| 36 | console.log( |
| 37 | ' ' + |
| 38 | label.padEnd(labelWidth) + |
| 39 | ' ' + |
| 40 | fmtVal(va) + |
| 41 | ' ' + |
| 42 | fmtVal(vb) + |
| 43 | ' ' + |
| 44 | fmtPct(pct) + |
| 45 | ' ' + |
| 46 | fmtFactor(va, vb) |
| 47 | ); |
| 48 | } |
| 49 | if (note) { |
| 50 | console.log(' (%s)', note); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | module.exports = {printGrid}; |