News for hackers. Stuff that matters.
Re:A love letter to the CSV format by dtech on Thursday March 27, 2025 @01:43AM (#43490667)
Most good parsers have an option to parse to integers or arbitrary precision decimals.
Re:A love letter to the CSV format by VMG on Thursday March 27, 2025 @06:02AM (#43491910)
Agreed. Which means that Javascript does not have a good parser.
Re:A love letter to the CSV format by exogen on Thursday March 27, 2025 @06:36AM (#43492127)
`JSON.parse` actually does give you that option via the `reviver` parameter, which gives you access to the original string of digits (to pass to `BigInt` or the number type of your choosing) – so per this conversation fits the "good parser" criteria.
Re:A love letter to the CSV format by whizzter on Thursday March 27, 2025 @09:44AM (#43493598)
Sadly the reviver parameter is a new invention only recently available in FF and Node, not at all in Safari.

Naturally not that hard to write a custom JSON parser but the need itself is a bad thing.

Re:A love letter to the CSV format by hajile on Thursday March 27, 2025 @09:56AM (#43493720)
Re:A love letter to the CSV format by arnorhs on Thursday March 27, 2025 @03:05PM (#43496928)
No it's been there for ages. Finalized as part of ecmascript 5

What you are probably thinking of is the context parameter of the reviver callback. That is relatively recent and mostly a qol improvement

Re:A love letter to the CSV format by whizzter on Thursday March 27, 2025 @04:59PM (#43498098)
Sorry yes, i was thinking of the context object with source parameter.

The issue it solves is a big one though, since without it the JSON.parse functionality cannot parse numbers that are larger than 64bit float numbers (f.ex. bigints).

Re:A love letter to the CSV format by hajile on Thursday March 27, 2025 @09:54AM (#43493698)
To be specific (if anyone was curious), you can force BigInt with something like this:

    //MAX_SAFE_INTEGER is actually 9007199254740991 which is 16 digits
    //you can instead check if exactly 16 and compare size one string digit at a time if absolute precision is desired.
    const bigIntReviver = (key, value, context) => typeof value === 'number' && Math.floor(value) === value && context.source.length > 15 ? BigInt(context.source) : value
      

    const jsonWithBigInt = x => JSON.parse(x, bigIntReviver)
Generally, I'd rather throw if a number is unexpectedly too big otherwise you will mess up the types throughout the system (the field may not be monomorphic) and will outright fail if you try to use math functions not available to BigInts.
Re:A love letter to the CSV format by VMG on Friday March 28, 2025 @04:18AM (#43502829)