Kaizen
Browse modulesData tabledatatable/sharedFunctions

Function: toCsv()

function toCsv<C extends string>(columns: readonly C[], rows: readonly Record<C, string>[]): string;

Defined in: shared/datatable/lib/export-csv.ts:47

Serialize rows to an RFC-4180 CSV string with a leading header row.

  • Generic over the column union C so callers get compile-time enforcement that every row carries the required keys. Each required key's presence is validated at runtime too (TypeScript's excess-property check only fires on fresh object literals); extra keys on a row are ignored, so a caller may pass a wider record shape and serialize a subset.
  • Formula-injection safe: a cell beginning with = + - @ (or a tab/CR/LF) is prefixed with a single quote so spreadsheet software treats it as text.
  • Every field is quoted, embedded quotes are doubled, rows join with CRLF. Output is deterministic. UTF-8, no BOM. Empty rows yields the header only.

For the DataTable case, prefer rowsToCsv, which derives the columns and cell text from a column config. Reach for toCsv directly when the caller already holds string records (e.g. a client-side results grid).

Type Parameters

Type Parameter
C extends string

Parameters

ParameterType
columnsreadonly C[]
rowsreadonly Record<C, string>[]

Returns

string

On this page