Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Trailing whitespace and empty headers #120

Closed
gmisra opened this issue Jan 6, 2017 · 4 comments
Closed

Trailing whitespace and empty headers #120

gmisra opened this issue Jan 6, 2017 · 4 comments

Comments

@gmisra
Copy link

gmisra commented Jan 6, 2017

Under the following conditions,

  • Header row included
  • Header row ends with a delimiter + whitespace
  • Last data row is not an empty line
  • Last data row ends with a delimiter + whitespace
  • The trim option is enabled.

The parser will return an error, that the last row does not have the correct number of columns. It appears that the trim behavior is inconsistent between the header row and the last data row in the absence of a trailing newline. This may be an edge case, or this may be the desired behavior, but it was surprising to me. Please feel free to close this issue if this is the desired behavior.

Sample Code (using csv-parse@1.1.7)

const parse = require('csv-parse');

const opts = {
  delimiter: ',',
  columns: true,
  trim: true,
  skip_empty_lines: true
};

const testA = 'h1,h2,h3, \n1,2,3, \n4,5,6, \n';
const testB = 'h1,h2,h3, \n1,2,3, \n4,5,6, ';

function test(desc, payload) {
  parse(payload, opts, function (err, res) {
    if (err) {
      console.log(`${desc}: ${err}`);
    } else {
      console.log(`${desc}: Success!`);
    }
  });
}

test('TEST A', testA);
test('TEST B', testB);
@wdavidw
Copy link
Member

wdavidw commented Jan 9, 2017

thanks for reporting

@gmisra
Copy link
Author

gmisra commented Jan 9, 2017

Thanks, can you clarify whether this is a bug or expected behavior? We have an external data source that provides files formatted like "testB" above. We currently append an extra newline, and I would like to document whether that is a workaround, or the correct behavior for using csv-parse.

@wdavidw
Copy link
Member

wdavidw commented Jan 9, 2017

Now that there is a test enforcing it, I would call it a feature.

@gmisra
Copy link
Author

gmisra commented Jan 9, 2017

👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants