114 lines
3.2 KiB
TypeScript
114 lines
3.2 KiB
TypeScript
export function addDays(date: Date, days: number): Date {
|
|
const copy = new Date(date);
|
|
copy.setHours(0, 0, 0, 0);
|
|
copy.setDate(copy.getDate() + days);
|
|
return copy;
|
|
}
|
|
|
|
export function formatYmd(date: Date): string {
|
|
return `${date.getFullYear()}${String(date.getMonth() + 1).padStart(2, "0")}${String(date.getDate()).padStart(2, "0")}`;
|
|
}
|
|
|
|
export function formatIsoDate(date: Date): string {
|
|
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
|
|
}
|
|
|
|
export function formatRuDate(date: Date): string {
|
|
return `${String(date.getDate()).padStart(2, "0")}.${String(date.getMonth() + 1).padStart(2, "0")}.${date.getFullYear()}`;
|
|
}
|
|
|
|
export function mondayOfWeek(date: Date): Date {
|
|
const copy = new Date(date);
|
|
copy.setHours(0, 0, 0, 0);
|
|
const mondayBasedDay = (copy.getDay() + 6) % 7;
|
|
copy.setDate(copy.getDate() - mondayBasedDay);
|
|
return copy;
|
|
}
|
|
|
|
export function sundayOfWeek(date: Date): Date {
|
|
return addDays(mondayOfWeek(date), 6);
|
|
}
|
|
|
|
export function formatRuWeekRange(date: Date): string {
|
|
return `${formatRuDate(mondayOfWeek(date))} - ${formatRuDate(sundayOfWeek(date))}`;
|
|
}
|
|
|
|
export function vvoMjzScheduleDates(): {
|
|
start: Date;
|
|
end: Date;
|
|
leg1: Date;
|
|
leg2: Date;
|
|
altLeg1: Date;
|
|
altLeg2: Date;
|
|
preStart: Date;
|
|
startYmd: string;
|
|
endYmd: string;
|
|
leg1Ymd: string;
|
|
leg2Ymd: string;
|
|
altLeg1Ymd: string;
|
|
altLeg2Ymd: string;
|
|
leg1Iso: string;
|
|
leg2Iso: string;
|
|
altLeg1Iso: string;
|
|
altLeg2Iso: string;
|
|
} {
|
|
const start = mondayOfWeek(addDays(new Date(), 7));
|
|
const end = sundayOfWeek(start);
|
|
const leg1 = start;
|
|
const leg2 = addDays(start, 1);
|
|
const altLeg1 = addDays(start, 4);
|
|
const altLeg2 = addDays(start, 5);
|
|
const preStart = addDays(start, -2);
|
|
|
|
return {
|
|
start,
|
|
end,
|
|
leg1,
|
|
leg2,
|
|
altLeg1,
|
|
altLeg2,
|
|
preStart,
|
|
startYmd: formatYmd(start),
|
|
endYmd: formatYmd(end),
|
|
leg1Ymd: formatYmd(leg1),
|
|
leg2Ymd: formatYmd(leg2),
|
|
altLeg1Ymd: formatYmd(altLeg1),
|
|
altLeg2Ymd: formatYmd(altLeg2),
|
|
leg1Iso: formatIsoDate(leg1),
|
|
leg2Iso: formatIsoDate(leg2),
|
|
altLeg1Iso: formatIsoDate(altLeg1),
|
|
altLeg2Iso: formatIsoDate(altLeg2),
|
|
};
|
|
}
|
|
|
|
export function vvoMjzRouteUrl(route = "VVO-MJZ"): string {
|
|
const dates = vvoMjzScheduleDates();
|
|
return `/ru-ru/schedule/route/${route}-${dates.startYmd}-${dates.endYmd}`;
|
|
}
|
|
|
|
export function vvoMjzDetailsUrl(): string {
|
|
const dates = vvoMjzScheduleDates();
|
|
return `/ru-ru/schedule/VVO/SU5752-${dates.leg1Ymd}/KJA/SU6837-${dates.leg2Ymd}/MJZ?request=schedule-route-VVO-MJZ-${dates.startYmd}-${dates.endYmd}`;
|
|
}
|
|
|
|
export function replaceVvoMjzFixtureDates(text: string): string {
|
|
const dates = vvoMjzScheduleDates();
|
|
const replacements: Array<[string, string]> = [
|
|
["20260518", dates.leg1Ymd],
|
|
["20260519", dates.leg2Ymd],
|
|
["20260522", dates.altLeg1Ymd],
|
|
["20260523", dates.altLeg2Ymd],
|
|
["20260524", dates.endYmd],
|
|
["2026-05-18", dates.leg1Iso],
|
|
["2026-05-19", dates.leg2Iso],
|
|
["2026-05-22", dates.altLeg1Iso],
|
|
["2026-05-23", dates.altLeg2Iso],
|
|
["2026-05-24", formatIsoDate(dates.end)],
|
|
];
|
|
|
|
return replacements.reduce(
|
|
(result, [from, to]) => result.replaceAll(from, to),
|
|
text,
|
|
);
|
|
}
|