d3/d3-dsv

wrong parsing d3.tsvParse with only number header

samuelvaneck opened this issue · 4 comments

I have an issue with headers that contain only numbers. It's not being parsed correctly by d3.tsvParse. The header with the number has the values of the first column.

The problem is with the last column 1234

E.g.
data string:
Distance_from_anode[nm] TAPC mCBP 4CzIPN-Me T2T 1234
70 0 0 0 0 0.000304539
71 0 0 0 0 0.000234767
72 0 0 0 0 0.00016237

is returns following incorrect result
index 70
4CzIPN-Me: 1e-99
1234: 70
Distance_from_anode[nm]: 1e-99
T2T: 0.000304539
TAPC: 1e-99
mCBP: 1e-99

index 71
4CzIPN-Me: 1e-99
1234: 71
Distance_from_anode[nm]: 1e-99
T2T: 0.000234767
TAPC: 1e-99
mCBP: 1e-99

index 72
4CzIPN-Me: 1e-99
1234: 72
Distance_from_anode[nm]: 1e-99
T2T: 0.00016237
TAPC: 1e-99
mCBP: 1e-99

Expected
index 70
4CzIPN-Me: 1e-99
1234: 0.000304539
Distance_from_anode[nm]: 70
T2T: 1e-99
TAPC: 1e-99
mCBP: 1e-99

index 71
4CzIPN-Me: 1e-99
1234: 0.000234767
Distance_from_anode[nm]: 71
T2T: 1e-99
TAPC: 1e-99
mCBP: 1e-99

index 72
4CzIPN-Me: 1e-99
1234: 0.00016237
Distance_from_anode[nm]: 72
T2T: 1e-99
TAPC: 1e-99
mCBP: 1e-99

I'm fixing if for now to check if the header has only number and adding an underscore in that's true.

Fil commented

Sorry I can't reproduce the issue: https://observablehq.com/d/f47916b62d483682

I tried again. It fails the code in my local dev enviroment but success on the observerablehq.com website. Not sure why for now. I'll futher investigate the issue.

It could be related to the fact that you can't use dot notation if an object key begins with a number:

const invalidObj = { 1234: 0.000304539 } // error, 1234 cannot be the key unless cast as a string

const validObj = { '1234': 0.000304539 } // Valid, but can't access with dot notation
console.log(validObj.1234) // error
console.log(validObj['1234']) // 0.000304539 // must access the value using brackets

const alsoValid = { '1234 this phrase with spaces can also be an object key': 0.000304539 } // another example

Closing since we can’t reproduce.