I got a csv buffer that I am sending to a function. In the buffer I got this data:
name,age
tom,33
pal,22
tim,60
jon,12
I use a node stream
that pipes it to an object with csv-parser
and as I go I want to see if the condition is met, if we find tim
whos age is already above 59 I want it to stop so jon is never read, and I want this function to return a string.
import { Readable } from 'stream'
import csv from 'csv-parser'
export default async function (buffer) {
Readable.from(buffer)
.pipe(csv())
.on('data', (data) => {
if (data.age > 59)
return 'stop'
})
}
This only works if I push it to an array, read the entire file and then after on end I return something.
I tried createReadStream
but does not work with the buffer and I get and error ENAMETOOLONG:
, with stream it works well but cannot stop on.('data')