d3 / d3-dsv

A parser and formatter for delimiter-separated values, such as CSV and TSV.
https://d3js.org/d3-dsv
ISC License
436 stars 76 forks source link

wrong parsing d3.tsvParse with only number header #77

Closed samuelvaneck closed 4 years ago

samuelvaneck commented 4 years ago

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 4 years ago

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

samuelvaneck commented 4 years ago

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.

stahlmanDesign commented 4 years ago

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
mbostock commented 4 years ago

Closing since we can’t reproduce.