fix: prevent silent precision loss for integers beyond MAX_SAFE_INTEGER#155
fix: prevent silent precision loss for integers beyond MAX_SAFE_INTEGER#155terminalchai wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe update adds a safety guard to prevent silent precision loss when parsing numeric strings that exceed JavaScript's maximum safe integer limit (Number.MAX_SAFE_INTEGER). In strict mode, the function throws an error; otherwise, it returns the original string value unchanged. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Closes #152
destrusesJSON.parseto convert numeric-looking strings to numbers. When a string represents an integer larger thanNumber.MAX_SAFE_INTEGER,JSON.parsesilently loses precision:This affects large IDs, counters, and any integer-valued string coming through Nitro/Nuxt
runtimeConfig, h3 query params, etc.Fix
After
JSON.parsesucceeds, if the result is a non-safe integer (!Number.isSafeInteger(result)), return the original string instead of the corrupted number:<= Number.MAX_SAFE_INTEGER) continue to parse as numbers — no behaviour change.safeDestrthrows a descriptiveSyntaxErrorfor large integers rather than silently corrupting.Tests
Two new test cases added to
test/index.test.ts:destrreturns the original string for9007199254740992,9007199254740993,-9007199254740993safeDestrthrows"[destr] Loss of precision for large integer string"in strict modeAll 24 tests pass.
Summary by CodeRabbit