From 704ff31e090a664d75436bce511a9864a7174f8a Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Fri, 27 May 2022 13:14:39 +0200 Subject: [PATCH] Handle query params with all delimiters --- lib/url-join.js | 4 ++-- test/tests.js | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/url-join.js b/lib/url-join.js index 37f1303..f2b24a9 100644 --- a/lib/url-join.js +++ b/lib/url-join.js @@ -50,8 +50,8 @@ function normalize (strArray) { // remove trailing slash before parameters or hash str = str.replace(/\/(\?|&|#[^!])/g, '$1'); - // replace ? in parameters with & - var parts = str.split('?'); + // replace ? and & in parameters with & + var parts = str.split(/(?:\?|&)+/); str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&'); return str; diff --git a/test/tests.js b/test/tests.js index 7e3dc48..62f701e 100644 --- a/test/tests.js +++ b/test/tests.js @@ -82,6 +82,12 @@ describe('url join', function () { urlJoin('http://example.org/x', '?a=1', '?b=2', '?c=3', '?d=4') .should.eql('http://example.org/x?a=1&b=2&c=3&d=4'); + + urlJoin('http:', 'www.google.com///', 'foo/bar', '&test=123', '&key=456') + .should.eql('http://www.google.com/foo/bar?test=123&key=456'); + + urlJoin('http:', 'www.google.com///', 'foo/bar', '&test=123', '?key=456') + .should.eql('http://www.google.com/foo/bar?test=123&key=456'); }); it('should merge slashes in paths correctly', function () {