Skip to content

Commit

Permalink
perf(withFragment): early return when no hash changes required
Browse files Browse the repository at this point in the history
this removed implicit removal behavior -- before initial util release
  • Loading branch information
pi0 committed Feb 5, 2024
1 parent 712b8d5 commit d6ce037
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/utils.ts
Expand Up @@ -300,6 +300,9 @@ export function isEqual(a: string, b: string, options: CompareURLOptions = {}) {
}

export function withFragment(input: string, hash: string): string {
if (!hash || hash === "#") {
return input;
}
const parsed = parseURL(input);
parsed.hash = hash === "" ? "" : "#" + encodeHash(hash);
return stringifyParsedURL(parsed);
Expand Down
7 changes: 1 addition & 6 deletions test/utilities.test.ts
Expand Up @@ -273,11 +273,6 @@ describe("withFragment", () => {
out: "https://example.com#foo",
},
{ input: "https://example.com", fragment: "", out: "https://example.com" },
{
input: "https://example.com#bar",
fragment: "",
out: "https://example.com",
},
{
input: "https://example.com#bar",
fragment: "0",
Expand All @@ -301,7 +296,7 @@ describe("withFragment", () => {
];

for (const t of tests) {
test(`${t.input} + ${t.fragment}`, () => {
test(`${JSON.stringify(t.input)} + ${JSON.stringify(t.fragment)}`, () => {
expect(withFragment(t.input, t.fragment)).toBe(t.out);
});
}
Expand Down

0 comments on commit d6ce037

Please sign in to comment.