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="); });