node-csv-parse icon indicating copy to clipboard operation
node-csv-parse copied to clipboard

Expose columns in info object

Open moroine opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe.

When using column: true option, we need to get the columns and it would be useful to have them as part as info object.

Describe the solution you'd like

Currently, I'm doing parser.options.columns because I saw in the code that options.columns is updated when reading the header line.

Thanks for this amazing CSV parser ❤️

moroine avatar Apr 21 '21 03:04 moroine

I just push a commit which expose columns inside the info/context object with the cast, info and on_record options. I don't wish to enrich state with columns because I am sort of planning to remove state in the next major release and merge its properties inside state.

Could you have a look at #81c5c8798de8d44446f69b210674fd74c7fe3d7a, in particular the test which illustrate its access and let me know if that works for you. If this is the case, I'll release a new minor version.

wdavidw avatar May 20 '21 20:05 wdavidw

Yeah, that's perfect thanks 👌

moroine avatar May 24 '21 05:05 moroine

Thanks for this, it's nice to have.

However, I'm interesting in having the information about columns before starting the for await of loop. Here is what I would like to do:

const parser = sourceStream.pipe(csv_parse({ columns: true, info: true }));
if (expectedColumnsAreFound(parser))
    for await (const {record, info} of parser)
    {
        // process records
    }

Of course, I could move the columns validity test inside the loop, but it would be tested for all records which is a waste of computation time in huge files. Any suggestion is most welcome.

obones avatar Jun 24 '21 10:06 obones