Skip to content

Commit

Permalink
Fix passing empty fragmentIdentifier to stringifyUrl()
Browse files Browse the repository at this point in the history
Fixes #380
  • Loading branch information
sindresorhus committed Feb 26, 2024
1 parent 50271f2 commit cc232cb
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ export function stringifyUrl(object, options) {
}

let hash = getHash(object.url);
if (object.fragmentIdentifier) {
if (typeof object.fragmentIdentifier === 'string') {
const urlObjectForFragmentEncode = new URL(url);
urlObjectForFragmentEncode.hash = object.fragmentIdentifier;
hash = options[encodeFragmentIdentifier] ? urlObjectForFragmentEncode.hash : `#${object.fragmentIdentifier}`;
Expand Down
1 change: 1 addition & 0 deletions test/stringify-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ test('stringify URL with fragment identifier', t => {
t.is(queryString.stringifyUrl({url: 'https://foo.bar', query: {}}), 'https://foo.bar');
t.is(queryString.stringifyUrl({url: 'https://foo.bar', query: {}, fragmentIdentifier: 'foo bar'}), 'https://foo.bar#foo%20bar');
t.is(queryString.stringifyUrl({url: 'https://foo.bar/', query: {}, fragmentIdentifier: '/foo/bar'}), 'https://foo.bar/#/foo/bar');
t.is(queryString.stringifyUrl({url: 'https://foo.bar/#foo', query: {}, fragmentIdentifier: ''}), 'https://foo.bar/');
});

test('skipEmptyString:: stringify URL with a query string', t => {
Expand Down

0 comments on commit cc232cb

Please sign in to comment.