| "use strict"; |
|
|
| const O = Object |
| , { first, strlen } = require ('printable-characters') |
| , limit = (s, n) => (first (s, n - 1) + '…') |
|
|
| const asColumns = (rows, cfg_) => { |
| |
| const |
|
|
| zip = (arrs, f) => arrs.reduce ((a, b) => b.map ((b, i) => [...a[i] || [], b]), []).map (args => f (...args)), |
|
|
| |
|
|
| cells = rows.map (r => r.map (c => c.replace (/\n/g, '\\n'))), |
|
|
| |
|
|
| cellWidths = cells.map (r => r.map (strlen)), |
| maxWidths = zip (cellWidths, Math.max), |
|
|
| |
|
|
| cfg = O.assign ({ |
| delimiter: ' ', |
| minColumnWidths: maxWidths.map (x => 0), |
| maxTotalWidth: 0 }, cfg_), |
|
|
| delimiterLength = strlen (cfg.delimiter), |
|
|
| |
|
|
| totalWidth = maxWidths.reduce ((a, b) => a + b, 0), |
| relativeWidths = maxWidths.map (w => w / totalWidth), |
| maxTotalWidth = cfg.maxTotalWidth - (delimiterLength * (maxWidths.length - 1)), |
| excessWidth = Math.max (0, totalWidth - maxTotalWidth), |
| computedWidths = zip ([cfg.minColumnWidths, maxWidths, relativeWidths], |
| (min, max, relative) => Math.max (min, Math.floor (max - excessWidth * relative))), |
|
|
| |
|
|
| restCellWidths = cellWidths.map (widths => zip ([computedWidths, widths], (a, b) => a - b)) |
|
|
| |
|
|
| return zip ([cells, restCellWidths], (a, b) => |
| zip ([a, b], (str, w) => (w >= 0) |
| ? (cfg.right ? (' '.repeat (w) + str) : (str + ' '.repeat (w))) |
| : (limit (str, strlen (str) + w))).join (cfg.delimiter)) |
| } |
|
|
| const asTable = cfg => O.assign (arr => { |
|
|
| |
|
|
| if (arr[0] && Array.isArray (arr[0])) { |
| return asColumns (arr.map (r => r.map ( |
| (c, i) => (c === undefined) ? '' : cfg.print (c, i) |
| ) |
| ), |
| cfg).join ('\n') |
| } |
|
|
| |
|
|
| const colNames = [...new Set ([].concat (...arr.map (O.keys)))], |
| columns = [colNames.map (cfg.title), |
| ...arr.map (o => colNames.map ( |
| key => (o[key] === undefined) ? '' : cfg.print (o[key], key) |
| ) |
| ) |
| ], |
| lines = asColumns (columns, cfg) |
|
|
| return (cfg.dash ? [lines[0], cfg.dash.repeat (strlen (lines[0])), ...lines.slice (1)] : lines).join ('\n') |
|
|
| }, cfg, { |
|
|
| configure: newConfig => asTable (O.assign ({}, cfg, newConfig)), |
| }) |
|
|
| module.exports = asTable ({ |
|
|
| maxTotalWidth: Number.MAX_SAFE_INTEGER, |
| print: String, |
| title: String, |
| dash: '-', |
| right: false |
| }) |
|
|