function serializeCell(value, delimiter) { const raw = String(value ?? ''); let prefix = 0; while ( prefix < raw.length && (raw.charCodeAt(prefix) <= 32 || raw.charCodeAt(prefix) === 0xfeff) ) prefix++; const formulaLike = /^[=+\-@]/.test(raw.slice(prefix)); const numeric = /^-?\d+(?:\.\d+)?$/.test(raw); const text = formulaLike && !numeric ? `'${raw}` : raw; return /[",\r\n]/.test(text) || text.includes(delimiter) ? `"${text.replaceAll('"', '""')}"` : text; } /** Serializes a comma-separated cell and neutralizes spreadsheet formulas. */ export function csvCell(value) { return serializeCell(value, ','); } /** Serializes a cell for a locale-specific delimited export. */ export function csvCellWithDelimiter(value, delimiter) { return serializeCell(value, delimiter); }