Skip to content

Fix unparse crash on null/undefined single-cell rows with skipEmptyLines - #1164

Merged
pokoli merged 2 commits into
mholt:masterfrom
SahinurDEV:fix-unparse-null-skip-empty-lines
Oct 8, 2026
Merged

pokoli merged 2 commits into
mholt:masterfrom
SahinurDEV:fix-unparse-null-skip-empty-lines

Conversation

@SahinurDEV

Copy link
Copy Markdown
Contributor

Fixes #1163

Problem

With skipEmptyLines: true, Papa.unparse() throws when an array row has exactly one cell and that cell is null or undefined:

Papa.unparse([[null], ['hello']], { skipEmptyLines: true });
// TypeError: Cannot read properties of null (reading 'length')

Papa.unparse([[undefined], ['hello']], { skipEmptyLines: true });
// TypeError: Cannot read properties of undefined (reading 'length')

The same crash happens with the { data: [...] } input form when no fields are given (and with JSON-string input), because those go through the same code path. With skipEmptyLines: false or 'greedy' there is no crash.

Cause

In serialize(), when there are no header fields and skipEmptyLines is true, a row counts as empty when it has one cell and that cell has a length of 0:

data[row].length === 1 && data[row][0].length === 0

null and undefined have no length property, so reading it throws. Other code already treats these values as empty: safe() writes null/undefined as empty cells, and the 'greedy' check uses join(''), which turns them into ''.

Fix

The true check now counts a single null or undefined cell as empty, the same as '':

data[row].length === 1 && (data[row][0] === null || data[row][0] === undefined || data[row][0].length === 0)

So [[null], ['hello']] and [[undefined], ['hello']] now return hello, the same as [[''], ['hello']]. This is a one-line change. Nothing changes for skipEmptyLines: false or 'greedy', and a single non-string cell like 0 is still not treated as empty.

I also checked the object input forms (array of objects, and {fields, data} with non-empty fields). They don't crash: when there's a header, skipEmptyLines: true only skips rows with no keys ({}), so this code path never runs. I didn't change their behavior.

Tests

I added these cases to UNPARSE_TESTS in tests/test-cases.js:

  • null/undefined single-cell rows are skipped with skipEmptyLines: true (array input)
  • the same with { data: [...] } input
  • null/undefined single-cell rows still produce empty lines with skipEmptyLines: false
  • null/undefined single-cell rows are skipped with skipEmptyLines: 'greedy'
  • a single-cell row containing 0 is not skipped with skipEmptyLines: true

Without the fix, the first two fail with the TypeError. With the fix:

  • npm run lint passes
  • npm run test-node: 260 passing, 25 pending
  • npm run test-mocha-headless-chrome: 264 passing, 8 pending

I didn't regenerate papaparse.min.js, since the project's history only updates it in version-bump commits.

When skipEmptyLines is true, unparse detects an empty array row by
checking whether its only cell has a length of 0. A null or undefined
cell has no length property, so the check threw a TypeError instead of
treating the row as empty. Treat null and undefined like an empty
string there, matching how they are already serialized as empty cells.

Fixes #1163
Comment thread tests/test-cases.js Resolved
@SahinurDEV
SahinurDEV requested a review from pokoli October 8, 2026 10:30
@pokoli
pokoli merged commit b1ca63c into mholt:master Oct 8, 2026
3 checks passed
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.

unparse crashes on a null cell when skipEmptyLines is true

2 participants