tdast-util-to-csv
tdast utility to serialize tdast into CSV (RFC-4180 compliant).
Install
npm install tdast-util-to-csv
Use
; const tdast = type: 'table' children: type: 'row' index: 0 children: type: 'column' index: 0 value: 'col1' type: 'column' index: 1 value: 'col2' type: 'column' index: 2 value: 'col3' type: 'row' index: 1 children: type: 'cell' columnIndex: 0 rowIndex: 1 value: 'row2cell1' type: 'cell' columnIndex: 1 rowIndex: 1 value: 'row2cell2' type: 'cell' columnIndex: 2 rowIndex: 1 value: 'row2cell3' ; ;
API
toCsv(tdast[, options])
Interface
;
Serializes a tdast Table
node to RFC-4180 compliant CSV.
All CSV field values are properly escaped with double quotes whenever applicable. If the tdast Cell
and Column
node contains non-string values, these are stringified with JSON.stringify
and propery escaped.
toCsv
automatically infers Column
node values to add in the CSV. You may override this behavior by specifying columns explicitly with options.columns
. You can also always ensure field values are enclosed with double quotes by configuring options.doubleQuotes
. These are detailed in the example below.
Example
Using the same tdast tree in the earlier example
; const options = columns: 'colA' 'colB' 'colC' doubleQuotes: true; ;
An example demonstrating how non-string values are stringified with JSON.stringify
and properly escaped:
;; // string, number, boolean, null values are easily stringified; // undefined value is converted to an empty string; // array values are stringified and properly escaped with double quotes; // object values are stringified and properly escaped with double quotes;
Related interfaces