Skip to content

Commit

Permalink
fix(stringifyParsedURL): check query existence in search (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
divine authored Jun 30, 2021
1 parent db5c1f6 commit dd8bc35
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function parseHost (input: string = ''): ParsedHost {
}

export function stringifyParsedURL (parsed: ParsedURL) {
const fullpath = parsed.pathname + (parsed.search ? '?' + parsed.search : '') + parsed.hash
const fullpath = parsed.pathname + (parsed.search ? (parsed.search.startsWith('?') ? '' : '?') + parsed.search : '') + parsed.hash
if (!parsed.protocol) {
return fullpath
}
Expand Down
24 changes: 23 additions & 1 deletion test/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasProtocol, isRelative } from '../src'
import { hasProtocol, isRelative, parsePath, stringifyParsedURL } from '../src'

describe('hasProtocol', () => {
const tests = [
Expand Down Expand Up @@ -34,3 +34,25 @@ describe('isRelative', () => {
})
}
})

describe('stringifyParsedURL', () => {
const tests = [
{ input: '.#hash', out: '.#hash' },
{ input: '.?foo=123', out: '.?foo=123' },
{ input: './?foo=123#hash', out: './?foo=123#hash' },
{ input: '/test?query=123#hash', out: '/test?query=123#hash' },
{ input: 'test?query=123#hash', out: 'test?query=123#hash' },
{ input: '/%c', out: '/%c' },
{ input: '/%', out: '/%' },
{ input: 'http://foo.com/test?query=123#hash', out: 'http://foo.com/test?query=123#hash' },
{ input: 'http://localhost:3000', out: 'http://localhost:3000' },
{ input: 'http://my_email%40gmail.com:[email protected]_site.com', out: 'http://my_email%40gmail.com:[email protected]_site.com' },
{ input: '/test?query=123,123#hash, test', out: '/test?query=123,123#hash, test' }
]

for (const t of tests) {
test(t.input.toString(), () => {
expect(stringifyParsedURL(parsePath(t.input))).toBe(t.out)
})
}
})

0 comments on commit dd8bc35

Please sign in to comment.