Skip to content

Commit

Permalink
fix(parseURL): support url with leading ? (resolves #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 17, 2021
1 parent 500885b commit aab7d5f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/parse.ts
Expand Up @@ -25,7 +25,7 @@ export function parseURL (input: string = '', defaultProto?: string): ParsedURL
}

const [protocol = '', auth, hostAndPath] = (input.match(/([^:/]+:)?\/\/([^/@]+@)?(.*)/) || []).splice(1)
const [host = '', path = ''] = (hostAndPath.match(/([^/]*)(.*)?/) || []).splice(1)
const [host = '', path = ''] = (hostAndPath.match(/([^/?]*)(.*)?/) || []).splice(1)
const { pathname, search, hash } = parsePath(path)

return {
Expand Down
3 changes: 2 additions & 1 deletion test/query.test.ts
Expand Up @@ -19,7 +19,8 @@ describe('withQuery', () => {
query: { str: '&', str2: '%26' },
out: '/?str=%26&str2=%2526'
},
{ input: '/?x=1,2,3', query: { y: '1,2,3' }, out: '/?x=1,2,3&y=1,2,3' }
{ input: '/?x=1,2,3', query: { y: '1,2,3' }, out: '/?x=1,2,3&y=1,2,3' },
{ input: 'http://a.com?v=1', query: { x: 2 }, out: 'http://a.com?v=1&x=2' }
]

for (const t of tests) {
Expand Down
1 change: 1 addition & 0 deletions test/url.test.ts
Expand Up @@ -4,6 +4,7 @@ describe('parseURL', () => {
const tests = [
{ input: '//test', out: { auth: '', hash: '', host: 'test', pathname: '', protocol: '', search: '' } },
{ input: 'https://test.com', out: { auth: '', hash: '', host: 'test.com', pathname: '', protocol: 'https:', search: '' } },
{ input: 'http://test.com?foo=bar', out: { auth: '', hash: '', host: 'test.com', pathname: '', protocol: 'http:', search: '?foo=bar' } },
{ input: '/test', out: { hash: '', pathname: '/test', search: '' } },
{ input: 'file:///home/user', out: { auth: '', hash: '', host: '', pathname: '/home/user', protocol: 'file:', search: '' } }
]
Expand Down

0 comments on commit aab7d5f

Please sign in to comment.