Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty last column does not get mapped #124

Closed
victorandree opened this issue Feb 28, 2019 · 0 comments
Closed

Empty last column does not get mapped #124

victorandree opened this issue Feb 28, 2019 · 0 comments

Comments

@victorandree
Copy link
Contributor

victorandree commented Feb 28, 2019

  • Operating System: macOS 10.14
  • Node Version: 11.6.0
  • NPM Version: 6.7.0
  • csv-parser Version: 2.1.0

Expected Behavior

I expect all values to be sent through mapValues, so I can change e.g. empty strings to null.

Actual Behavior

If the final column is empty, i.e. the final character on the line is ,, then the last value is not sent through mapValues.

How Do We Reproduce?

Save as index.js:

const csv = require('csv-parser');
const { Readable } = require('stream');

const dummy = `a,b,c
1,2,
3,4,5
6,,
`;

const s = new Readable();
s.push(dummy);
s.push(null);

const rows = [];
s.pipe(
  csv({
    mapValues: ({ value, index }) => {
      console.log(index);
      return value === '' ? null : value;
    },
  }),
)
  .on('data', row => rows.push(row))
  .on('end', () => {
    console.log(rows);
  });

Run node index.js:

0
1
0
1
2
0
1
[ Row { a: '1', b: '2', c: '' },
  Row { a: '3', b: '4', c: '5' },
  Row { a: '6', b: null, c: '' } ]

mapValues does not run for the first and final rows, leaving it as empty string. This is obviously happening because of https://github.com/mafintosh/csv-parser/blob/master/index.js#L182 simply inserting a this._empty value. Skipping mapValue for this value is not consistent with other columns.

victorandree pushed a commit to victorandree/csv-parser that referenced this issue Feb 28, 2019
This is a one line fix to address mafintosh#124. I could write a test, as well.
victorandree pushed a commit to victorandree/csv-parser that referenced this issue Mar 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant