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

Make the Row TypeScript return type generic #9

Merged
merged 2 commits into from
Feb 12, 2020

Conversation

Ethan-Arrowood
Copy link
Contributor

When using mapValues, you can essentially transform the result of the csv parsing. In order to reflect these changes, one should be able to pass a generic to the neatCsv method that will be used as the resulting data type.

index.d.ts Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

Even better would be to make https://github.com/mafintosh/csv-parser/blob/789e0c13e2cbd705dbdbe263adbdc0493e9b90ec/index.d.ts#L57 generic, so the value defined as return value for the mapValues option, if defined, ends up as the main return value. Can you open an issue/PR on https://github.com/mafintosh/csv-parser about that? I'm happy to merge this in the meantime though.

@Ethan-Arrowood
Copy link
Contributor Author

Yes happy to do so!

Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
@Ethan-Arrowood
Copy link
Contributor Author

Ethan-Arrowood commented Feb 12, 2020

Even better would be to make https://github.com/mafintosh/csv-parser/blob/789e0c13e2cbd705dbdbe263adbdc0493e9b90ec/index.d.ts#L57 generic, so the value defined as return value for the mapValues option, if defined, ends up as the main return value

What is the goal here? I don't think I'm fully understanding the expected result

/* data.csv
num
1
2
3
4
*/
const csv = require('csv-parser')
const fs = require('fs')

type Row = {
  num: number
}

const results: Row[] = [];

fs.createReadStream('data.csv')
  .pipe(csv<Row>({
    mapValues: ({header, index, value}) => {
      var parsed = parseInt(value)
      return isNaN(parsed) ? -1 : parsed
    }
  }))
  .on('data', (data) => results.push(data)) // is the type of `data` supposed to be `Row`?
  .on('end', () => {
    console.log(results);
    // [
    //   { num: 1 },
    //   { num: 2 },
    //   { num: 3 },
    //   { num: 4 }
    // ]
  });

Because I don't believe that is possible with Node.js types. In this type of code I'd use type casting to indicate what data is, i.e. .on('data', (data) => results.push(data as Row))

@sindresorhus
Copy link
Owner

My thinking was that mapValues could change the return value of csv(), but I forgot it has to go through a loosely-typed stream in csv-parser, so never mind.

@sindresorhus sindresorhus changed the title Feature: make Row result generic Make the Row TypeScript return type generic Feb 12, 2020
@sindresorhus sindresorhus merged commit 2048173 into sindresorhus:master Feb 12, 2020
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

Successfully merging this pull request may close these issues.

2 participants