Skip to content
Data TableAPI Reference

Core CSV & JSON export

comins-table@0.2.2React

0.2.0: Types and helpers on this page use the React-state contract from the root comins-table entry. Neutral /core has a separate type contract. See the migration guide for imports and cell policies.

Converts supplied rows and export column definitions into text. It does not automatically query selected rows, pages or server data, or trigger a download.

export declare function exportCominsRowsToCsv<TData>({ columnOrder, columns, headerOverrides, rows, metadata, valueSource, }: CominsExportRowsOptions<TData>): string;

Joins one header row and data rows with newlines. Values containing commas, quotes or CR/LF use CSV quoting. null/undefined becomes empty; objects use JSON text. Header precedence is headerOverrides[id] → label → id → empty string.

export declare function exportCominsRowsToJson<TData>({ columnOrder, columns, headerOverrides, rows, metadata, valueSource, }: CominsExportRowsOptions<TData>): string;

Uses the same resolved headers as object keys and returns a JSON array with two-space indentation. Later columns overwrite repeated keys, so use unique headers. Values follow JSON.stringify rules; cyclic references and bigint can throw.

export type CominsExportFormat = "csv" | "json";

csv/json format tokens for selecting the corresponding export function.

export type CominsExportValueSource = "formatted" | "raw";

raw is the default and uses value. formatted uses format when available, falling back to value.

export type CominsExportColumn<TData> = {
format?: (row: TData, rowIndex: number) => unknown;
id?: string;
label?: string;
value: (row: TData, rowIndex: number) => unknown;
};
Property Behavior / default
format Optional conversion for formatted mode.
id Optional ID referenced by columnOrder/headerOverrides.
label Optional header text.
value Required raw-value resolver using the row and its index in the supplied array.
export type CominsExportRowsOptions<TData> = {
columnOrder?: string[];
columns: Array<CominsExportColumn<TData>>;
headerOverrides?: Record<string, string>;
rows: readonly TData[];
metadata?: CominsExportMetadata<TData>;
valueSource?: CominsExportValueSource;
};
Property Behavior / default
columnOrder A nonempty list exports only those IDs, in that order. Omitted/empty uses all columns in their original order.
columns Required export column definitions, separate from React display columns.
headerOverrides Header overrides by column ID; also used as JSON keys.
rows Required rows to export; the caller chooses the scope.
metadata Optional management-column getters; see below.
valueSource raw (default) or formatted.

CSV import and structure builders share the same contracts through comins-table/core, without React or DOM.

Import/export guide

export declare function importCominsRowsFromCsv<TData>({ text, mapRow, hasHeader, maxCharacters, maxCells, }: CominsCsvImportOptions<TData>): TData[];

Validates all CSV syntax and field counts before mapping application rows. Syntax errors throw SyntaxError; invalid or exceeded limits throw RangeError. Mapper errors propagate without returning partial rows. Side effects cannot be rolled back. Does not read files, generate IDs, update tables or restore Tree/Group structures.

export type CominsCsvImportRow = {
cells: readonly string[];
headers: readonly string[] | null;
rowIndex: number;
};

Read-only mapper input. Headers do not create properties or field paths automatically; duplicate headers are preserved.

Property Behavior/default
cells Read-only raw cells of the current record.
headers First record by default; null when hasHeader=false.
rowIndex Zero-based data-row index excluding the header.
export type CominsCsvImportOptions<TData> = {
text: string;
mapRow: (row: CominsCsvImportRow) => TData;
hasHeader?: boolean;
maxCharacters?: number;
maxCells?: number;
};

Supports CRLF/LF/CR records and doubled quotes. Preserves quoted newlines, spaces, leading zeroes and formula text. Ignores an initial BOM and trailing separator. Empty/header-only input returns an empty array. Actual empty records remain and must match field counts. Processing is synchronous in memory; values are not sanitized for formulas or HTML.

Property Behavior/default
text Required CSV text. The caller decodes file bytes.
mapRow Required pure row mapper. Validate headers, values and business rules here.
hasHeader Defaults to true; false treats the first record as data.
maxCharacters Defaults to 1,000,000 UTF-16 code units including BOM. Requires a positive safe integer.
maxCells Defaults to 100,000 cells including headers. Requires a positive safe integer.
export type CominsExportMetadata<TData> = {
__rowId?: (row: TData, rowIndex: number) => CominsRowId;
__parentId?: (row: TData, rowIndex: number) => CominsRowId | null;
__depth?: (row: TData, rowIndex: number) => number;
__groupId?: (row: TData, rowIndex: number) => CominsRowId | null;
};

Appends enabled management columns after business columns in __rowId, __parentId, __depth, __groupId order without mutating rows. Conflicting business headers throw. Business-column ordering, labels and formatting do not modify management columns. JSON preserves null and numeric/string IDs; CSV renders null as an empty cell.

Property Behavior/default
__rowId Row ID getter.
__parentId Parent ID getter; null for roots.
__depth Depth getter; zero for roots.
__groupId Group ID getter.
export type CominsExportTreeOptions<TData> = Omit<CominsExportRowsOptions<TData>, "rows" | "metadata"> & {
nodes: readonly import("./tree").CominsTreeNode<TData>[];
getRowId: (row: TData, sourceIndex: number) => CominsRowId;
};

Options for exporting the full input tree. Inherits business-column options without inspecting UI collapse, filtering or selection.

Property Behavior/default
nodes Required original Tree nodes.
getRowId Required row ID getter. Tree uses preorder indices; Group uses original indices.
export type CominsExportGroupedOptions<TData, TGroup> = Omit<CominsExportRowsOptions<TData>, "metadata"> & {
groups: readonly TGroup[];
getGroupId: (group: TGroup) => CominsRowId;
getRowGroupId: (row: TData, sourceIndex: number) => CominsRowId;
getRowId: (row: TData, sourceIndex: number) => CominsRowId;
};

Options for exporting membership in single-level groups. The caller supplies rows and groups.

Property Behavior/default
groups Required groups determining export order.
getGroupId Required group ID getter.
getRowGroupId Required row group ID getter.
getRowId Required row ID getter. Tree uses preorder indices; Group uses original indices.
export declare function createCominsTreeExportOptions<TData>({ nodes, getRowId, ...options }: CominsExportTreeOptions<TData>): CominsExportRowsOptions<TData>;

Visits every input node in preorder, including collapsed children. Attaches __rowId, __parentId and __depth; roots have null parent and zero depth. getRowId receives preorder indices. Rejects duplicate IDs and cycles. Returns CSV/JSON options preserving original row references. Do not separately reorder or replace returned rows; regenerate options after data or structure changes.

export declare function createCominsGroupedExportOptions<TData, TGroup>({ rows, groups, getGroupId, getRowGroupId, getRowId, ...options }: CominsExportGroupedOptions<TData, TGroup>): CominsExportRowsOptions<TData>;

Orders groups as supplied and preserves original input order within groups. Attaches __rowId and __groupId. ID getters use original indices; column value/format use final export indices. Rejects duplicate group/row IDs and unknown groups. Does not add synthetic rows, headings or summaries for empty groups. Use returned rows and options together and regenerate after changes.