1

I'm using the csv-parse Node package to parse a CSV string. The documentation suggests that I read the parsed result using something like this:

const output = []
parse(`
  "1","2","3"
  "a","b","c"
`)
.on('readable', function() {
  let record
  while (record = this.read()) {
    output.push(record)
  }
})

This approach angers the linting gods by assigning within a while loop (and having an unnamed function). It also just doesn't make me feel great to begin with; I have a feeling there is a more concise and readable approach somewhere.

How can I populate a parsed result from the csv-parse stream without resorting to a while loop?

slifty
  • 13,062
  • 13
  • 71
  • 109

1 Answers1

1

Since it's a ReadableStream you can use on('data', () => {}) instead if you prefer.

.on('data', (record) => output.push(record))

In any case, there's nothing wrong with that code, and it's the recommended approach by the csv-parse developers.

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • Thanks! I appreciate that the original code was fine; I admit I strongly prefer what you offered in this answer, though. – slifty Jan 02 '20 at 19:36