Skip to content

Keep the last row of a one-column CSV without a trailing newline - #9237

Open
Abhist17 wants to merge 2 commits into
processing:mainfrom
Abhist17:fix/csv-last-row
Open

Abhist17 wants to merge 2 commits into
processing:mainfrom
Abhist17:fix/csv-last-row

Conversation

@Abhist17

@Abhist17 Abhist17 commented Oct 3, 2026

Copy link
Copy Markdown

Resolves #9222

Changes:

  • src/io/csv.js: at the end of input, the parser now flushes the pending value whenever it's partway through a value (state !== 0), not only when the row already has an entry. So one-column rows keep their last value, and a file holding a single value parses to one row. A trailing newline still ends in state 0 with an empty entry, so no phantom row is added.

  • src/io/files.js: loadTable() builds an empty table from an empty file instead of reading data[0].length on no rows. With header, the columns fall back to [] instead of undefined, as suggested in the issue.

  • test/unit/io/csv.js (new): parse() tests for:

    • a one-column file without a trailing newline,
    • a single value, plain and quoted,
    • an empty line between rows,
    • a trailing newline,
    • multi-column rows and a trailing empty field,
    • the empty string.

    Four of them fail on main.

  • test/unit/io/loadTable.js: three loadTable() tests with new assets (one-column file, single value, empty file).

parse() before and after:

input before after
"a\nb" [["a"]] [["a"],["b"]]
"only", "42" [] [["only"]], [["42"]]
"\"only\"" [] [["only"]]
"a\n\nb" [["a"],[""]] [["a"],[""],["b"]]
"a\n", "x,y\n1,2", "x,", "" unchanged unchanged

npx vitest run --project unit-tests test/unit/io/csv.js test/unit/io/loadTable.js: 19 passed.

PR Checklist

parse() only flushed the pending value when the current row already had
an entry, which a one-column row never has until its newline. The last
row was dropped, a single-value file parsed to no rows, and loadTable()
then threw reading data[0].length. Flush whenever the parser is in the
middle of a value, and give an empty file an empty table.

Resolves processing#9222
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.

[p5.js 2.0+ Bug Report]: loadTable() drops the last row of a one-column CSV without a trailing newline

1 participant