Something went wrong. Try again.
Smoking habit tracker
Something went wrong. Try again.
1234567891011121314151617181920212223242526// Shared CSV helpers for cig export — RFC4180 with minimal quoting.// Extracted to deduplicate export.get/post handlers and integration tests.
// Quote a single field per RFC4180 (double quotes escaped as "").export function csvEscape(v: string): string { return `"${v.replace(/"/g, '""')}"`}
export interface CigExportRow { id: number timestamp: number // ms since epoch (handler already converts sec -> ms) date: string // ISO string cigTypeId: number | null}
// Build CSV for cig export rows. Header + RFC4180 quoting when field contains// comma, quote, newline, or equals sign. Byte-identical to previous handler output.export function buildCigCsv(rows: CigExportRow[]): string { let csv = 'id,timestamp,date,cig_type_id\n' for (const row of rows) { const vals = [String(row.id), String(row.timestamp), row.date, row.cigTypeId != null ? String(row.cigTypeId) : ''] csv += vals.map((v) => (/["\n,=]/.test(v) ? csvEscape(v) : v)).join(',') + '\n' } return csv}