tcsv

0.1.5 • Public • Published

tcsv

CSV parser/stringifier. Follows RFC 4180 in its output and is fairly permissive in its input.

Install

$ npm i tcsv

Use

const tcsv = require('tcsv')

parseRaw(string)

Separates a string into an row/column data (an array of row arrays).

tcsv.parseRaw(`
abc,def,ghi
jkl,"m n o",pqr`)
/* [ [ 'abc', 'def', 'ghi' ], [ 'jkl', 'm n o', 'pqr' ] ] */

parse(string, [fields = null, [asObject = false]])

Separates a string into structured data (an array of Map records).

tcsv.parse(`
last,first,age
Smith,Alice,34
Doe,John,25`)
/* [
  Map { 'last' => 'Smith', 'first' => 'Alice', 'age' => '34' },
  Map { 'last' => 'Doe', 'first' => 'John', 'age' => '25' },
] */

Pass fields to return only those named columns. Pass asObject to use raw Object instances instead of Maps. If you are parsing user data and want Objects instead of Maps, make sure to pass a whitelist of fields.

tcsv.parse(`
last,first,age
Smith,Alice,34
Doe,John,25`, ['last', 'age'], true)
/* [ { last: 'Smith', age: '34' }, { last: 'Doe', age: '25' } ] */

stringifyRaw(array)

Serializes row/column data (an array of row arrays) to a string.

tcsv.stringifyRaw([
  [ 'abc', 'def', 'ghi' ],
  [ 'jkl', 'm n o', 'pqr' ],
])
/* 'abc,def,ghi\r\njkl,m n o,pqr' */

stringify(array)

Serializes structured data (an array of Map or Object records) to a string.

tcsv.stringify([
  { last: 'Smith', first: 'Alice', age: '34' },
  { last: 'Doe', first: 'John', age: '25' },
])
/* 'last,first,age\r\nSmith,Alice,34\r\nDoe,John,25' */

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i tcsv

      Weekly Downloads

      8

      Version

      0.1.5

      License

      MIT

      Last publish

      Collaborators

      • nfd