papertrail/lib/csvExport.ts
caelum0x 695a365ae4 feat: PaperTrail — enterprise evidence-verification platform
Multi-tenant platform (182 pages, 228 API routes, ~30 modules) built on a
deterministic verification core: clinical-trial claims checked against
ClinicalTrials.gov registered results and raw event counts (ARR/NNT/RR+CI
recomputed in code, oracle-tested), code-enforced exact-span grounding, and
multi-source cross-verification. Auth/RBAC/multi-tenancy, document processing
at scale (unpdf + Docling), agentic workflows, Claude Science connector,
compliance/audit, billing, public API, and more.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 07:21:22 +03:00

27 lines
1.2 KiB
TypeScript

// Turns an array of row objects into an RFC-4180-ish CSV string a researcher can open
// in Excel/Sheets. Pure and deterministic: field order follows the caller-supplied
// `columns`, missing fields become empty strings, and any field containing a comma,
// double-quote, CR, or LF is wrapped in double quotes with interior quotes doubled.
function escapeField(value: string): string {
const needsQuoting = /[",\r\n]/.test(value);
if (!needsQuoting) return value;
return `"${value.replace(/"/g, '""')}"`;
}
function toCell(value: string | number | undefined): string {
if (value === undefined) return "";
return escapeField(String(value));
}
/**
* Build a CSV document from `rows`, emitting exactly the given `columns` (in order) as
* both the header row and the value order for every data row. Rows are separated by CRLF
* per RFC 4180. Empty `rows` still yields the header line.
*/
export function toCsv(rows: Record<string, string | number>[], columns: string[]): string {
const header = columns.map((column) => escapeField(column)).join(",");
const body = rows.map((row) => columns.map((column) => toCell(row[column])).join(","));
return [header, ...body].join("\r\n");
}