Created
March 11, 2024 19:46
-
-
Save arleighdickerson/1bd24ee8f94503930e8f550495b7474b to your computer and use it in GitHub Desktop.
Utilities for working with preact-router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { exec, getCurrentUrl } = require('preact-router'); | |
export namespace urlUtil { | |
export const getUrlParameter = (name: string, location: Pick<Location, 'pathname' | 'search'>): string | null => { | |
if (typeof window === 'undefined') { | |
return null; // shim | |
} | |
const a = 'http://localhost' /* <-- dummy string for parsing */ + location.pathname + location.search; | |
const url = new URL(a); | |
return url.searchParams.has(name) ? url.searchParams.get(name) : null; | |
}; | |
export const isMatching = (route: string) => { | |
return exec(route, getCurrentUrl(), {}) !== false; | |
}; | |
export const isActive = (route: string) => { | |
return exec(getCurrentUrl(), route, {}) !== false; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment