UNPKG

cspell

Version:
32 lines 1.07 kB
import chalk from 'chalk'; import strip from 'strip-ansi'; export function tableToLines(table, deliminator) { deliminator = deliminator || table.deliminator || ' | '; const columnWidths = []; const { header, rows } = table; function recordWidths(row) { row.forEach((col, idx) => { columnWidths[idx] = Math.max(strip(col).length, columnWidths[idx] || 0); }); } function justifyRow(c, i) { return c + ' '.repeat(columnWidths[i] - strip(c).length); } function toLine(row) { return decorateRowWith(row, justifyRow).join(deliminator); } function* process() { yield toLine(decorateRowWith(header, headerDecorator)); yield* rows.map(toLine); } recordWidths(header); rows.forEach(recordWidths); return [...process()]; } function headerDecorator(t) { return chalk.bold(chalk.underline(t)); } export function decorateRowWith(row, ...decorators) { return decorators.reduce((row, decorator) => row.map(decorator), row); } //# sourceMappingURL=table.js.map