From 549d6777e460a047f3a89216cbd5a58864906229 Mon Sep 17 00:00:00 2001 From: Deepak S Patwardhan Date: Wed, 10 Jun 2020 23:54:23 -0600 Subject: [PATCH] Fix sorting existing query params in URL when `sort` option is false (#265) Co-authored-by: Sindre Sorhus --- index.js | 2 +- test/stringify-url.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f8ca0df7..de1d7b18 100644 --- a/index.js +++ b/index.js @@ -361,7 +361,7 @@ exports.stringifyUrl = (input, options) => { const url = removeHash(input.url).split('?')[0] || ''; const queryFromUrl = exports.extract(input.url); - const parsedQueryFromUrl = exports.parse(queryFromUrl); + const parsedQueryFromUrl = exports.parse(queryFromUrl, {sort: false}); const query = Object.assign(parsedQueryFromUrl, input.query); let queryString = exports.stringify(query, options); diff --git a/test/stringify-url.js b/test/stringify-url.js index 98055cc1..2b3c3728 100644 --- a/test/stringify-url.js +++ b/test/stringify-url.js @@ -52,3 +52,7 @@ test('stringify URL from the result of `parseUrl` with query string that contain const parsedUrl = queryString.parseUrl(url); t.deepEqual(queryString.stringifyUrl(parsedUrl, {encode: false}), url); }); + +test('stringify URL without sorting existing query params', t => { + t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?C=3&A=1', query: {D: 4, B: 2}}, {sort: false}), 'https://foo.bar?C=3&A=1&D=4&B=2'); +});