Skip to content

Commit 104c05d

Browse files
deepview-autofixclaudeChALkeR
authored
fix: encode large negative numbers in stringify
Numbers with magnitude >= 1e21 are serialized by JavaScript in exponential notation (e.g. -1e+22). The previous check only compared value < 1e21, so negative numbers in this range skipped encodeString and left a raw '+' in the output, which query string parsers decode as a space and corrupt the round-trip. Use Math.abs to apply the threshold symmetrically for positive and negative values. Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: DeepView Autofix <276251120+deepview-autofix@users.noreply.github.com> Co-Authored-By: Nikita Skovoroda <chalkerx@gmail.com> Signed-off-by: Nikita Skovoroda <chalkerx@gmail.com>
1 parent 9dcbaf2 commit 104c05d

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

lib/stringify.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function getAsPrimitive(value) {
1111
} else if (type === "bigint" || type === "boolean") {
1212
return "" + value;
1313
} else if (type === "number" && Number.isFinite(value)) {
14-
return value < 1e21 ? "" + value : encodeString("" + value);
14+
return Math.abs(value) < 1e21 ? "" + value : encodeString("" + value);
1515
}
1616

1717
return "";

test/stringify.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ test("should coerce numbers to string", () => {
9494
assert.strictEqual(qs.stringify({ foo: -72.42 }), "foo=-72.42");
9595
assert.strictEqual(qs.stringify({ foo: Number.NaN }), "foo=");
9696
assert.strictEqual(qs.stringify({ foo: 1e21 }), "foo=1e%2B21");
97+
assert.strictEqual(qs.stringify({ foo: -1e21 }), "foo=-1e%2B21");
9798
assert.strictEqual(qs.stringify({ foo: Number.POSITIVE_INFINITY }), "foo=");
9899
});
99100

0 commit comments

Comments
 (0)