-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigate.ts
executable file
·50 lines (40 loc) · 1.34 KB
/
navigate.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
import { navigate as sr_navigate } from "svelte-routing";
import { getWeek, getWeekNumber, getWeekYear, WEEK_IN_MS } from "./week";
export const getFromEnv = (key: string): string => {
try {
// @ts-ignore
const { env } = process ?? {};
const value = env[key] ?? "";
return String(value);
} catch (_) {
return "";
}
};
const withoutTrailingSlash = (path: string) =>
path.endsWith("/") ? path.slice(0, -1) : path;
export const getBasePath = () => {
const public_url = getFromEnv("PUBLIC_URL");
const urlMatch = public_url.match(/\:\/\/[^/]+(.*)/);
if (!urlMatch) {
return withoutTrailingSlash(public_url);
}
return withoutTrailingSlash(urlMatch[1]);
};
export const withBasePath = (target: string) => {
const prefix = target.startsWith("/") ? getBasePath() : "";
return `${prefix}${target}`;
};
export const navigate = (
target: string,
options: { replace?: boolean; state?: object }
): void => sr_navigate(withBasePath(target), options);
export const getWeekRoute = (
year: number,
weekNumber: number,
i: number
): string => {
const thursday = getWeek(year, weekNumber)[3];
const relatedThursday = new Date(thursday.getTime() + i * WEEK_IN_MS);
return `/${getWeekYear(relatedThursday)}/${getWeekNumber(relatedThursday)}`;
};
export const getYearRoute = (year: number, i = 0): string => `/${year + i}`;