adaltas / node-csv

Full featured CSV parser with simple api and tested against large datasets.
https://csv.js.org
MIT License
4.05k stars 267 forks source link

The last line is always skipped #393

Closed C-Ezra-M closed 1 year ago

C-Ezra-M commented 1 year ago

Describe the bug

The last line is always skipped. CSV 6.3.1 is used in this example, which uses csv-parse 5.4.0.

To Reproduce

function getParser() {
    let parser = csv.parse()
    parser.on('readable', () => {
        let rec
        while ((rec = parser.read()) !== null) {
            console.log(rec)
        }
    })
    return parser
}

let src = `color,hex,user,score,gold,silver,bronze,status
Teal,#009c9c,Alec,46581,3,2,3,S
Navy,#000084,Laser,44037,0,6,2,S
Forest,#002c00,Rice Field,40797,1,1,1,S
Yellow,#ffff00,Keyacom,38171,2,0,2,S
`
getParser().write(src)
getParser().write(src + "\n")

To parse all records successfully without introducing a dummy record, at least two trailing newlines are required.

The getParser().write(src) will parse the first 4 lines. The getParser().write(src + "\n") will parse all 5 lines.

Additional context

This happens regardless of the columns or skip_empty_lines option.

wdavidw commented 1 year ago

You open a stream writer, write into it but you don't close it.

C-Ezra-M commented 1 year ago

Yeah, I forgot.