unjs / destr

🚀 Faster, secure and convenient alternative for JSON.parse
MIT License
1.03k stars 14 forks source link

parsing decimals incorrectly #93

Closed kricsleo closed 8 months ago

kricsleo commented 8 months ago

Environment

destr: 2.0.1(latest) nodejs: 18.18.1

Reproduction

const value = Math.random().toString() // => '0.5207845927625354'
destr(value) // => '0.5207845927625354' ❌ should be number
JSON.parse(value) // => 0.5207845927625354 🟢 correct

Describe the bug

Parsing a string of numbers with more than 14 decimals gives incorrect results, it should return the number but actually returns the original string.

This should have something to do with the regex here:

https://github.com/unjs/destr/blob/37210516ccef951dcc870f17a5abee52122a3122/src/index.ts#L8

Additional context

No response

Logs

No response

pi0 commented 8 months ago

Thanks for issue you are right checking docs, it seems 64bit js numbers can have up to 14 digits and 16 decimal places while regex is not allowing it. PR welcome to fix.