mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-07 01:14:34 -06:00
Split getMatchingTime into isTimeMatched for individual testing of timing configs, drop async
This commit is contained in:
@@ -50,7 +50,7 @@ export default async function handler(
|
|||||||
const now = localNow();
|
const now = localNow();
|
||||||
|
|
||||||
const config = await fetchConfiguration();
|
const config = await fetchConfiguration();
|
||||||
const matching = await getMatchingTime(config, now);
|
const matching = getMatchingTime(config, now);
|
||||||
|
|
||||||
// No matching time - no notification to send.
|
// No matching time - no notification to send.
|
||||||
if (matching == null) {
|
if (matching == null) {
|
||||||
|
|||||||
@@ -222,12 +222,10 @@ export const numberAsDay: Record<DayEnum, number> = {
|
|||||||
sunday: 0
|
sunday: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function getMatchingTime(
|
export function isTimeMatched(
|
||||||
config: Configuration,
|
time: TimeConfig,
|
||||||
now: Date = new Date()
|
now: Date = new Date()
|
||||||
): Promise<TimeConfig | null> {
|
): boolean {
|
||||||
const times = config.times.filter((time) => {
|
|
||||||
// If the day doesn't match, skip.
|
|
||||||
if (!time.days.has(dayAsNumber[now.getDay().toString()])) return false;
|
if (!time.days.has(dayAsNumber[now.getDay().toString()])) return false;
|
||||||
const nowTime = {
|
const nowTime = {
|
||||||
hours: now.getHours(),
|
hours: now.getHours(),
|
||||||
@@ -235,15 +233,18 @@ export async function getMatchingTime(
|
|||||||
};
|
};
|
||||||
|
|
||||||
const startTime = time.time;
|
const startTime = time.time;
|
||||||
const endTime = addTime(
|
const endTime = addTime(time.time, time.maxLate ?? { hours: 0, minutes: 0 });
|
||||||
time.time,
|
|
||||||
time.maxLate ?? { hours: 0, minutes: 0 }
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
compareTime(nowTime, startTime) >= 0 && compareTime(nowTime, endTime) <= 0
|
compareTime(nowTime, startTime) >= 0 && compareTime(nowTime, endTime) <= 0
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
export function getMatchingTime(
|
||||||
|
config: Configuration,
|
||||||
|
now: Date = new Date()
|
||||||
|
): TimeConfig | null {
|
||||||
|
const times = config.times.filter((time) => isTimeMatched(time, now));
|
||||||
|
|
||||||
// This shouldn't be thrown, if I did my job right.
|
// This shouldn't be thrown, if I did my job right.
|
||||||
if (times.length > 1)
|
if (times.length > 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user