Skip to content

Commit

Permalink
feat: add withoutFragment util (resolves #199)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 5, 2024
1 parent 6b00230 commit 11404da
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ withFragment("/foo#bar", "");
// /foo
```

### `withoutFragment`

Removes the fragment (or hash) part of URL.

```ts
withoutFragment("http://example.com/foo?q=123#bar");
// 'http://example.com/foo?q=123'
```

## License

[MIT](./LICENSE)
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,7 @@ export function withFragment(input: string, hash: string): string {
parsed.hash = hash === "" ? "" : "#" + encodeURI(hash);
return stringifyParsedURL(parsed);
}

export function withoutFragment(input: string): string {
return withFragment(input, "");
}
32 changes: 32 additions & 0 deletions test/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
withProtocol,
isScriptProtocol,
withFragment,
withoutFragment,
} from "../src";

describe("hasProtocol", () => {
Expand Down Expand Up @@ -305,3 +306,34 @@ describe("withFragment", () => {
});
}
});

describe("withoutFragment", () => {
const tests = [
{
input: "https://example.com#foo",
out: "https://example.com",
},
{
input: "https://example.com",
out: "https://example.com",
},
{
input: "/foo#bar",
out: "/foo",
},
{
input: "/foo/#bar",
out: "/foo/",
},
{
input: "/foo?bar#baz",
out: "/foo?bar",
},
];

for (const t of tests) {
test(`${t.input}`, () => {
expect(withoutFragment(t.input)).toBe(t.out);
});
}
});

0 comments on commit 11404da

Please sign in to comment.