From 104c05df96791f9719277d6139bfdf39063b4a3e Mon Sep 17 00:00:00 2001 From: DeepView Autofix <276251120+deepview-autofix@users.noreply.github.com> Date: Sun, 19 Apr 2026 04:33:56 +0300 Subject: [PATCH] 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 Co-Authored-By: DeepView Autofix <276251120+deepview-autofix@users.noreply.github.com> Co-Authored-By: Nikita Skovoroda Signed-off-by: Nikita Skovoroda --- lib/stringify.js | 2 +- test/stringify.test.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/stringify.js b/lib/stringify.js index a2dea7e..62a179d 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -11,7 +11,7 @@ function getAsPrimitive(value) { } else if (type === "bigint" || type === "boolean") { return "" + value; } else if (type === "number" && Number.isFinite(value)) { - return value < 1e21 ? "" + value : encodeString("" + value); + return Math.abs(value) < 1e21 ? "" + value : encodeString("" + value); } return ""; diff --git a/test/stringify.test.ts b/test/stringify.test.ts index dd3980a..2c0c6cb 100644 --- a/test/stringify.test.ts +++ b/test/stringify.test.ts @@ -94,6 +94,7 @@ test("should coerce numbers to string", () => { assert.strictEqual(qs.stringify({ foo: -72.42 }), "foo=-72.42"); assert.strictEqual(qs.stringify({ foo: Number.NaN }), "foo="); assert.strictEqual(qs.stringify({ foo: 1e21 }), "foo=1e%2B21"); + assert.strictEqual(qs.stringify({ foo: -1e21 }), "foo=-1e%2B21"); assert.strictEqual(qs.stringify({ foo: Number.POSITIVE_INFINITY }), "foo="); });