Skip to content

Instantly share code, notes, and snippets.

@arleighdickerson
Created March 11, 2024 19:46
Show Gist options
  • Save arleighdickerson/1bd24ee8f94503930e8f550495b7474b to your computer and use it in GitHub Desktop.
Save arleighdickerson/1bd24ee8f94503930e8f550495b7474b to your computer and use it in GitHub Desktop.
Utilities for working with preact-router
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