Keyang / node-csvtojson

Blazing fast and Comprehensive CSV Parser for Node.JS / Browser / Command Line.
MIT License
2.02k stars 271 forks source link

Incompatiblity with delimiter: 'auto' and noheader: true #120

Closed mbpreble closed 8 years ago

mbpreble commented 8 years ago

It appears that the options delimiter: 'auto', and noheader: true are not compatible with each other - csvtojson seems to decide to use "," as the delimiter in this case instead of detecting it in the first data row.

Minimal example (checkColumn is set to true to highlight that data is not split as expected):

  var Converter = require("csvtojson").Converter;
  var test_converter = new Converter({
    delimiter: 'auto',
    headers: ['col1', 'col2'],
    noheader: true,
    checkColumn: true
  });

  var my_data = 'first_val\tsecond_val';
  test_converter.fromString(my_data, function(err, result) {
    console.log(err);
    console.log(result);
  });

Output: row_process, Error: column_mismatched. Data: first_val\tsecond_val. Row index: 0

It's possible to work around this by prepending a fake header row to the data, specifying headers in the configuration, and skipping the noheader option:

var Converter = require("csvtojson").Converter;
  var test_converter = new Converter({
    delimiter: 'auto',
    headers: ['col1', 'col2'],
    noheader: false,
    checkColumn: true
  });

  var my_data = 'first_val\tsecond_val';
  my_data = 'header_val1, header_val2\n' + my_data;
  test_converter.fromString(my_data, function(err, result) {
    console.log(err);
    console.log(result);
  });

The above produces the expected output of [ { col1: 'first_val', col2: 'second_val' } ].

Keyang commented 8 years ago

thanks for pointing it out. I have fixed this in latest version (1.0.1)