1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
'use strict';
const routes = [];
export const route = (pattern, handler) => {
routes.push({
pattern: pattern,
handler: handler
});
};
export const go = (dest) => {
window.location.hash = '#' + dest;
};
export const start = () => {
const dest = window.location.hash.slice(1);
for (let route of routes) {
const match = route.pattern.exec(dest);
if (!match) continue;
return route.handler(match.slice(1));
}
};
window.addEventListener('hashchange', start);