From a89f5c21562fe75b083a270090eadfbe318e5954 Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Thu, 19 Mar 2015 18:51:55 -0400 Subject: [PATCH 1/2] querystring: parse numbers correctly Fixes a number parsing regression introduced in 85a92a3 Fixes: https://github.com/iojs/io.js/issues/1208 PR-URL: https://github.com/iojs/io.js/pull/1213 Reviewed-By: Rod Vagg Reviewed-By: Brian White --- lib/querystring.js | 3 +++ test/parallel/test-querystring.js | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/querystring.js b/lib/querystring.js index af320cf8f86397..f0d473a39ae24d 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -90,6 +90,9 @@ var hexTable = new Array(256); for (var i = 0; i < 256; ++i) hexTable[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase(); QueryString.escape = function(str) { + // replaces encodeURIComponent + // http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4 + str = '' + str; var len = str.length; var out = ''; var i, c; diff --git a/test/parallel/test-querystring.js b/test/parallel/test-querystring.js index e2591d709015f5..17fa6df9247747 100644 --- a/test/parallel/test-querystring.js +++ b/test/parallel/test-querystring.js @@ -138,6 +138,14 @@ qsWeirdObjects.forEach(function(testCase) { assert.equal(testCase[1], qs.stringify(testCase[0])); }); +// coerce numbers to string +assert.strictEqual('foo=0', qs.stringify({ foo: 0 })); +assert.strictEqual('foo=0', qs.stringify({ foo: -0 })); +assert.strictEqual('foo=3', qs.stringify({ foo: 3 })); +assert.strictEqual('foo=-72.42', qs.stringify({ foo: -72.42 })); +assert.strictEqual('foo=', qs.stringify({ foo: NaN })); +assert.strictEqual('foo=', qs.stringify({ foo: Infinity })); + // nested var f = qs.stringify({ a: 'b', From c9aec2b7167a08dc88141fbe3be1c498f8c5b061 Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Thu, 19 Mar 2015 21:33:04 -0400 Subject: [PATCH 2/2] querystring: fix broken stringifyPrimitive stringifyPrimitive has always failed to stringify numbers since its introduction in 422d3c9. This went uncaught due to encodeURIComponent's string coercion. Fixes: https://github.com/iojs/io.js/issues/1208 PR-URL: https://github.com/iojs/io.js/pull/1213 Reviewed-By: Rod Vagg Reviewed-By: Brian White --- lib/querystring.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/querystring.js b/lib/querystring.js index f0d473a39ae24d..09220a919e3edc 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -147,8 +147,10 @@ QueryString.escape = function(str) { }; var stringifyPrimitive = function(v) { - if (typeof v === 'string' || (typeof v === 'number' && isFinite(v))) + if (typeof v === 'string') return v; + if (typeof v === 'number' && isFinite(v)) + return '' + v; if (typeof v === 'boolean') return v ? 'true' : 'false'; return '';