npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tdast-util-to-csv

v0.1.1

Published

tdast utility to serialize tdast into CSV (RFC-4810 compliant)

Downloads

2

Readme

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