All files hash.ts

28.57% Statements 4/14
0% Branches 0/4
0% Functions 0/3
36.36% Lines 4/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 241x                     1x         1x           1x  
import { app, ROUTER_404_EVENT, ROUTER_EVENT } from "apprun";
 
// A router that handles hash based links. It provides no more functionality than the
// default AppRun router. URL is assumed to start with a # (unless it's absent).
function hashLinkRouter(url: string): void {
	if(!url) url = "#";
	const [name, ...rest] = url.split("/");
	app.run(name, ...rest) || app.run(ROUTER_404_EVENT, name, ...rest);
	app.run(ROUTER_EVENT, name, ...rest);
}
 
const hashLinkRouterPopstateHandler = (_event: Event) => {
	// app.route cannot be undefined/null as it is assigned at the end of this module.
	app.route!(location.hash);
};
 
document.addEventListener("DOMContentLoaded", () => {
	window.onpopstate = hashLinkRouterPopstateHandler;
	hashLinkRouter(location.hash);
});
 
// Overwrite AppRun's default router.
app.route = hashLinkRouter;