aboutsummaryrefslogtreecommitdiffstats
path: root/apps/browser-extension/src/utils/url.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/browser-extension/src/utils/url.ts')
-rw-r--r--apps/browser-extension/src/utils/url.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/apps/browser-extension/src/utils/url.ts b/apps/browser-extension/src/utils/url.ts
new file mode 100644
index 00000000..9ed423a0
--- /dev/null
+++ b/apps/browser-extension/src/utils/url.ts
@@ -0,0 +1,41 @@
+/**
+ * Check if a URL is an HTTP or HTTPS URL.
+ * @param url The URL to check.
+ * @returns True if the URL starts with "http://" or "https://", false otherwise.
+ */
+export function isHttpUrl(url: string) {
+ const lower = url.toLowerCase();
+ return lower.startsWith("http://") || lower.startsWith("https://");
+}
+
+/**
+ * Normalize a URL by removing the hash and trailing slash.
+ * @param url The URL to process.
+ * @param base Optional base URL for relative URLs.
+ * @returns Normalized URL as string.
+ */
+export function normalizeUrl(url: string, base?: string): string {
+ const u = new URL(url, base);
+ u.hash = ""; // Remove hash fragment
+ let pathname = u.pathname;
+ if (pathname.endsWith("/") && pathname !== "/") {
+ pathname = pathname.slice(0, -1); // Remove trailing slash except for root "/"
+ }
+ u.pathname = pathname;
+ return u.toString();
+}
+
+/**
+ * Compare two URLs ignoring hash and trailing slash.
+ * @param url1 First URL.
+ * @param url2 Second URL.
+ * @param base Optional base URL for relative URLs.
+ * @returns True if URLs match after normalization.
+ */
+export function urlsMatchIgnoringAnchorAndTrailingSlash(
+ url1: string,
+ url2: string,
+ base?: string,
+): boolean {
+ return normalizeUrl(url1, base) === normalizeUrl(url2, base);
+}