Skip to content

Commit 6412a0b

Browse files
committed
Address comments
1 parent 32749f4 commit 6412a0b

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

lib/utils.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,22 @@ function parseJSONWithBigInt(str) {
358358
const processed = str.replace(
359359
/(?:[:,[])\s*(-?\d+)(?=\s*[,}\]])/g,
360360
(match, number) => {
361-
const num = Number(number);
362-
// If the number is not safe, convert it to a string
363-
if (!Number.isSafeInteger(num)) {
364-
// Preserve the prefix character (: or , or [)
365-
const prefix = match.charAt(0);
366-
return `${prefix} "${number}"`;
361+
// Use BigInt to check if the number is safe without losing precision
362+
// BigInt can accurately represent arbitrarily large integers
363+
try {
364+
const bigIntValue = BigInt(number);
365+
// Check if the value is outside the safe integer range
366+
if (
367+
bigIntValue < BigInt(Number.MIN_SAFE_INTEGER) ||
368+
bigIntValue > BigInt(Number.MAX_SAFE_INTEGER)
369+
) {
370+
// Preserve the prefix character (: or , or [)
371+
const prefix = match.charAt(0);
372+
return `${prefix} "${number}"`;
373+
}
374+
} catch {
375+
// If BigInt conversion fails, keep the original match
376+
return match;
367377
}
368378
return match;
369379
}

0 commit comments

Comments
 (0)