-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathrouter-utils.ts
More file actions
38 lines (34 loc) · 1.01 KB
/
router-utils.ts
File metadata and controls
38 lines (34 loc) · 1.01 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
import { Router, UrlMatcher, UrlSegment } from '@angular/router';
const flatten = (url: UrlSegment[]): string => url.map((u) => u.path).join('/');
export function startsWith(prefix: string): UrlMatcher {
return (url: UrlSegment[]) => {
if (flatten(url).startsWith(prefix)) {
return { consumed: url };
}
return null;
};
}
export function endsWith(prefix: string): UrlMatcher {
return (url: UrlSegment[]) => {
if (flatten(url).endsWith(prefix)) {
return { consumed: url };
}
return null;
};
}
export function connectRouter(router: Router, useHash = false): void {
let url: string;
if (!useHash) {
url = `${location.pathname.substring(1)}${location.search}`;
router.navigateByUrl(url);
window.addEventListener('popstate', () => {
router.navigateByUrl(url);
});
} else {
url = `${location.hash.substring(1)}${location.search}`;
router.navigateByUrl(url);
window.addEventListener('hashchange', () => {
router.navigateByUrl(url);
});
}
}