diff --git a/lib/url-join.js b/lib/url-join.js index 58443aa..32c3b27 100644 --- a/lib/url-join.js +++ b/lib/url-join.js @@ -49,8 +49,8 @@ function normalize (strArray) { // remove trailing slash before parameters or hash str = str.replace(/\/(\?|&|#[^!])/g, '$1'); - // replace ? in parameters with & - const parts = str.split('?'); + // replace ? and & in parameters with & + const parts = str.split(/(?:\?|&)+/); str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&'); return str; diff --git a/test/tests.js b/test/tests.js index 2944fb1..72c8a4f 100644 --- a/test/tests.js +++ b/test/tests.js @@ -82,6 +82,12 @@ describe('url join', () => { 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', () => {