mini-csv

0.0.10 • Public • Published

npm version Build Status Coverage Status Code Climate Inch CI

Dependency Status devDependency Status

mini-csv

Parse/stringify data in CSV format

If you have different needs regarding the functionality, please add a feature request.

Installation

npm install --save mini-csv

Usage

File in1.csv:

boolean-column,number-column,string-column
false,0,string0
false,1,"string1"
true,2,"string2"
,,
null,null,null
undefined,undefined,undefined

File examples/test1.txt:

'use strict';

const fs = require('fs');
const csv = require('../');

const options = {
  header: true, // [ 'boolean', 'number', 'string' ],
  null: 'null',
  undefined: 'undefined',
  quote: '"',
  //escapeQuote: '\\',
  //commaNoLine: false, // ignore full line if any of the values contains comma
  //EOL: '\n',
  extraRows: [], // row indexes to ignore
};
const parse = csv.parse(options);

const in1 = fs.readFileSync('./in1.csv', 'utf8');
console.log('in1:\n', in1);

parse.on('error',  (error) => console.log(`* error`,   error));
parse.on('line',    (line)    => console.log(`* line`,    line));
parse.on('header', (header) => console.log(`* header`, header));
parse.on('row',    (row, header)    => console.log(`* row`,    row, header));
console.log('out1:\n', parse.parse(in1) );
const stringifyOptions = {
  header: true, // [ 'boolean', 'number', 'string' ],
  null: 'null',
  undefined: 'undefined',
  quote: '"',
  escapeQuote: '\\',
  commaNoLine: false, // ignore full line if any of the values contains comma
  EOL: '\n',
  extraRows: [ // row indexes to add
    //{ num: 1, value: '; this is extra row at line with index 1' }
  ],
};
const columns = [ 'boolean-column', 'number-column', 'string-column' ];

const stringify = csv.stringify(stringifyOptions);

const out2 = stringify.stringify(out1, columns);
console.log('\nout2:', out2);

Credits

Alexander

Links to package pages:

github.com   npmjs.com   travis-ci.org   coveralls.io   inch-ci.org

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i mini-csv

Weekly Downloads

1

Version

0.0.10

License

MIT

Unpacked Size

25.7 kB

Total Files

17

Last publish

Collaborators

  • alykoshin