Skip to content

Commit

Permalink
fix(joinURL): preserve params from input path
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 12, 2020
1 parent ad33243 commit 8f8f10a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ export function getParams (input: InputURL): SearchParams {
export function joinURL (input0: string, ...input: string[]): string {
const path = input.map(parseURL)
const baseURL = parseURL(input0)

for (const p of path) {
p.url.searchParams.forEach((value, key) => {
baseURL.url.searchParams.set(key, value)
})
}

baseURL.url.pathname = joinPath(baseURL.url.pathname, ...path.map(p => p.url.pathname))

return normalizeURL(baseURL)
}
3 changes: 2 additions & 1 deletion test/join.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ describe('joinURL', () => {
{ input: ['a', 'b/', 'c'], out: 'a/b/c' },
{ input: ['a', 'b/', '/c'], out: 'a/b/c' },
{ input: ['/a?foo=bar#123', 'b/', 'c/'], out: '/a/b/c/?foo=bar#123' },
{ input: ['http://foo.com', 'a'], out: 'http://foo.com/a' }
{ input: ['http://foo.com', 'a'], out: 'http://foo.com/a' },
{ input: ['a?p1=1', 'b?p2=2'], out: 'a/b?p1=1&p2=2' }
]

for (const t of tests) {
Expand Down

0 comments on commit 8f8f10a

Please sign in to comment.