Keyang / node-csvtojson

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

works incorectly with empty quoted values #265

Closed aeternitatus closed 6 years ago

aeternitatus commented 6 years ago

I've got an issue with empty values inside quotes, here is an example:

const csv = require('csvtojson');

const string = 'a|^^|^b^';

csv({
  delimiter: '|',
  quote: '^',
  noheader: true,
})
  .fromString(string)
  .then((jsonObj) => {
    console.log(jsonObj);
  });

prints [ { field1: 'a', field2: '^^', field3: 'b' } ] while my expected result is: [ { field1: 'a', field2: '', field3: 'b' } ]

took a look at the code, my guess is that isQuoteOpen condition should be fixed to check if everything value consists of are quotes, i.e. smth like: str[0] === quote && (str[1] === quote || str.length === 2)

therefore smth like:

RowSplit.prototype.isQuoteOpen = function (str) {
        var quote = this.quote;
        var escape = this.escape;
        return str[0] === quote && ((str[1] === quote || str.length === 2) || str[1] !== quote ||
            str[1] === escape && (str[2] === quote || str.length === 2));
    };

do you agree? any chances to get if fixed any soon? I'd really appreciate it...

btw, if I strip down an original condition in that function, then looks like possible option for isQuoteOpen to be true is str[0] === quote && str[1] === escape && str.length === 2, how possible is that value consists of two chars when first is quote and another once is escape..? tried few inputs:

  1. for 'a|^$|b^' I get [ { field1: 'a', field2: '$|^b' } ]
  2. for 'a|^$' I get an unclosed_quote exception
Keyang commented 6 years ago

Thanks for pointing this out.