Menu

CSV Is Not Just split(','): Parsing, Type Inference, and Time Alignment in the Browser
πŸ“°

CSV Is Not Just split(','): Parsing, Type Inference, and Time Alignment in the Browser

DEV Community: datascienceΒ·YananLeeΒ·8 days ago
#jifwVCLB
#devtodatascience#science#devto#dev#const
Long article (1841 words) β€” no AI summary yet.
Reading 0:00
15s threshold

The first version of every chart tool I've written had this line in it:

const rows = text . split ( '\n' ) . map ( line => line . split ( ',' ) ) ;

Enter fullscreen mode

Exit fullscreen mode

It works on the file you tested. It breaks on the first file a real person uploads: a product name with a comma in it, a Windows CRLF, a UTF-8 BOM, a cell with a newline. Then you discover the harder half of the problem β€” once you have rows, you still don't have series . Someone gives you three snapshots with different sets of categories and expects a smooth animation between them.

Here's the pipeline I ended up with, in plain JavaScript, no dependencies.

1. Parse CSV as a state machine, not a split

RFC 4180 gives you quoted fields, escaped quotes ( "" ), and newlines inside fields. A two-state loop handles all of it:

function parseCSV ( text ) { if ( text . charCodeAt ( 0 ) === 0xFEFF ) text = text .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.