csv-to-table Render CSV/TSV as aligned ASCII table
Examples
Render CSV as aligned ASCII table
Usage
"name,role,city
Alice,admin,Paris
Bob,editor,London
Charlie,v..." | csv-to-table Format product data as a table for a Slack message
Usage
"Product,Price,Stock
Widget Pro,$29.99,142
Gadget Plus,$49.99..." | csv-to-table Create a readable table for a plain-text status email
Usage
"Employee,Department,Status
Alice,Engineering,Active
Bob,Mark..." | csv-to-table View source
(input)=>{
const lines = input.trim().split("\n");
if (!lines.length) return "";
const delim = lines[0].includes("\t") ? "\t" : ",";
const rows = lines.map((l)=>l.split(delim).map((c)=>c.trim()));
const cols = Math.max(...rows.map((r)=>r.length));
const widths = Array(cols).fill(0);
for (const row of rows)for(let i = 0; i < cols; i++)widths[i] = Math.max(widths[i], (row[i] || "").length);
const fmt = (row)=>row.map((c, i)=>(c || "").padEnd(widths[i])).join(" \u2502 ");
const sep = widths.map((w)=>"\u2500".repeat(w)).join("\u2500\u253C\u2500");
const result = [
fmt(rows[0]),
sep
];
for(let i = 1; i < rows.length; i++)result.push(fmt(rows[i]));
return result.join("\n");
}