Skip to content

@mostlyrightmd/core/formats

Thrown when toonDumps receives rows that aren’t valid tabular input. The encoder refuses non-uniform rows or non-primitive values rather than silently dropping columns or stringifying nested structures.

new ToonTabularError(message, options): ToonTabularError

string = ""

ContractErrorOptions = {}

ToonTabularError

ContractError.constructor

readonly actual: null | string

ContractError.actual

static defaultErrorCode: string = "CONTRACT_ERROR"

Subclass override — the stable string enum surfaced via errorCode.

ContractError.defaultErrorCode

readonly docUrl: null | string

ContractError.docUrl

readonly errorCode: string

ContractError.errorCode

readonly expected: null | string

ContractError.expected

readonly field: null | string

ContractError.field

readonly location: null | string

ContractError.location

name: string = "ToonTabularError"

ContractError.name

readonly requestId: null | string

ContractError.requestId

readonly source: null | string

ContractError.source

toDict(): Record<string, unknown>

Return a JSON-safe dict suitable for MCP error.data.

Record<string, unknown>

ContractError.toDict

csvDumps(rows): string

Serialize rows to a CSV string. Column names come from Object.keys(rows[0]). Empty rows emits an empty string (matches pd.DataFrame({}).to_csv(index=False)).

Header cells are quoted on the same triggers as data cells. Without this guard, a column name like "a,b" would dump as two headers and roundtrip into the wrong schema.

readonly Record<string, unknown>[]

string


csvLoads(data): object

Parse a CSV string into rows + columns.

Returns string-valued cells; CSV is dtype-lossy (pandas read_csv would re-infer dtypes — we leave that to the caller).

Empty input → { rows: [], columns: [] }. Header-only input → { rows: [], columns: [...] }.

The stateful parser preserves newlines inside quoted cells, so csvLoads(csvDumps(rows)) is a faithful roundtrip when cells contain \n.

string

object

columns: string[]

rows: Record<string, string>[]


jsonDumps(rows, columns?): string

Serialize rows to a JSON string.

  • Non-empty: emits records form JSON.stringify(rows).
  • Empty: emits envelope {"columns": [...], "data": []} — column names survive the empty-frame roundtrip. Throws RangeError if columns is not provided in the empty case.

readonly Record<string, unknown>[]

readonly string[]

string


jsonLoads(data): object

Parse a JSON string into rows + column-order array.

Accepts both the records form ([{...}, ...]) and the empty-frame envelope ({columns, data}). Returns BOTH rows AND columns so callers can preserve column order on empty cases.

string

object

columns: string[]

rows: Record<string, unknown>[]


toonDumps(rows, columns?): string

Encode rows as a TOON v3.0 tabular block.

Header is rows[N]{c1,c2,...}:; data lines are 2-space indented and comma-separated. Empty rows emits rows[0]: (header-only, no columns region; matches Python _encode_array_field empty-list path).

Note: empty-row encoding differs from dumps() in toon.py (which carries column names through rows[0]{...}:). The TS encoder accepts a columns second arg in the empty case for parity with that pandas-aware wrapper.

readonly Record<string, unknown>[]

readonly string[]

string

when rows are non-uniform (differing key sets across rows) or when any cell value is non-primitive (object/array/bigint/etc.).


toonLoads(data): object

Parse a TOON v3.0 tabular block back into rows + columns.

Accepts only the tabular shape that toonDumps produces; nested objects / expanded lists are out of scope for the formats module.

string

object

columns: string[]

rows: Record<string, unknown>[]