UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

34 lines (33 loc) 1.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatMarkdownTable = formatMarkdownTable; /** * Build an oxfmt-compatible Markdown table with padded columns so that the * generated file passes `oxfmt --check` without a reformat pass. * * Column width = max(header length, longest cell length, 3). Cells are * padded with trailing spaces; the separator row uses `-` characters of * the same width. * * Character width note: uses JavaScript `.length` (UTF-16 code units), * which matches oxfmt's own accounting for typical BMP characters used * in VENDOR.md generation (em-dash `—`, backticks, version strings). * * @param headers Column headers (top row) * @param rows Data rows; each row must have `headers.length` cells * @returns Lines ready to concatenate with `\n` */ function formatMarkdownTable(headers, rows) { const columnCount = headers.length; const widths = headers.map((h, i) => { const cellMax = rows.reduce((max, row) => { var _a; return Math.max(max, ((_a = row[i]) !== null && _a !== void 0 ? _a : '').length); }, 0); return Math.max(h.length, cellMax, 3); }); const formatRow = (cells) => `| ${cells.map((cell, i) => cell.padEnd(widths[i])).join(' | ')} |`; const lines = [formatRow(headers), `| ${widths.map((w) => '-'.repeat(w)).join(' | ')} |`]; for (const row of rows) { const padded = Array.from({ length: columnCount }, (_, i) => { var _a; return (_a = row[i]) !== null && _a !== void 0 ? _a : ''; }); lines.push(formatRow(padded)); } return lines; }