Skip to content

tdast/tdast-util-to-csv

Repository files navigation

tdast-util-to-csv

tdast utility to serialize tdast into CSV (RFC-4180 compliant).


Install

npm install tdast-util-to-csv

Use

import toCsv from 'tdast-util-to-csv';

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',
        },
      ],
    },
  ],
};

expect(toCsv(tdast))
  .toEqual('col1,col2,col3\nrow2cell1,row2cell2,row2cell3');

API

toCsv(tdast[, options])

Interface

function toCsv(
  // tdast Table node
  tdast: Table,
  // options to configure serializer
  options?: Options,
): string;

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

import toCsv from 'tdast-util-to-csv';

const options = {
  columns: ['colA', 'colB', 'colC'],
  doubleQuotes: true,
};

expect(toCsv(tdast, options))
  .toEqual('"colA","colB","colC"\n"row2cell1","row2cell2","row2cell3"');

An example demonstrating how non-string values are stringified with JSON.stringify and properly escaped:

import td from 'tdastscript';
import toCsv from 'tdast-util-to-csv';

// string, number, boolean, null values are easily stringified
expect(
  toCsv(td('table', [td('row', ['one', 2, true, false, null])])),
).toEqual('one,2,true,false,null');

// undefined value is converted to an empty string
expect(
  toCsv(td('table', [td('row', [undefined, undefined, undefined])])),
).toEqual(',,');

// array values are stringified and properly escaped with double quotes
expect(
  toCsv(
    td('table', [
      td('row', [
        td('cell', { value: ['one', 2, true, false, null, undefined] }),
      ]),
    ]),
  ),
).toEqual('"[""one"",2,true,false,null,null]"');

// object values are stringified and properly escaped with double quotes
expect(
  toCsv(
    td('table', [
      td('row', [
        td('cell', {
          value: {
            one: 2,
            3: 'four',
            five: [6, 'seven'],
            eight: null,
            nine: undefined,
          },
        }),
      ]),
    ]),
  ),
).toEqual(
  '"{""3"":""four"",""one"":2,""five"":[6,""seven""],""eight"":null}"',
);

Related interfaces

interface Options {
  // array of column strings that will be used as object keys.  Overrides the column values detected in the tdast tree.
  columns?: string[];
  // if CSV field values should be doublequoted
  doubleQuotes?: boolean;
}

Related