mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-10 14:06:45 -06:00
Parse ^& transform timing data inside TimeConfigSchema, add DayEnum type
This commit is contained in:
138
src/timing.ts
138
src/timing.ts
@@ -15,15 +15,34 @@ const DayEnumSchema = z.enum([
|
|||||||
'saturday',
|
'saturday',
|
||||||
'sunday'
|
'sunday'
|
||||||
]);
|
]);
|
||||||
|
type DayEnum = z.infer<typeof DayEnumSchema>;
|
||||||
|
|
||||||
const TimeConfigSchema = z.object({
|
const TimeConfigSchema = z.object({
|
||||||
// A short name to be included in notifications
|
// A short name to be included in notifications
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
// The time this notification is intended for. e.g. "12:00", 24 hour time
|
// The time this notification is intended for. e.g. "12:00", 24 hour time
|
||||||
time: z.string().refine((time) => {
|
time: z.string().transform((time, ctx) => {
|
||||||
const { hours, minutes } = parseTime(time);
|
if (!time.match(/^[0-9]{2}:[0-9]{2}$/)) {
|
||||||
return isValidTime({ hours, minutes });
|
ctx.addIssue({
|
||||||
}, 'Invalid time format'),
|
code: z.ZodIssueCode.custom,
|
||||||
|
message: 'Time must be in the format "HH:MM" e.g. "12:00"'
|
||||||
|
});
|
||||||
|
return z.NEVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsed = parseTime(time);
|
||||||
|
|
||||||
|
if (!isValidTime(parsed)) {
|
||||||
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.custom,
|
||||||
|
message:
|
||||||
|
'Invalid time format. Max 23 hours, 59 minutes. Positives only.'
|
||||||
|
});
|
||||||
|
return z.NEVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsed;
|
||||||
|
}),
|
||||||
// The days this configuration is active on.
|
// The days this configuration is active on.
|
||||||
days: z.preprocess((v) => {
|
days: z.preprocess((v) => {
|
||||||
const parsedArray = z.any().array().parse(v);
|
const parsedArray = z.any().array().parse(v);
|
||||||
@@ -33,11 +52,30 @@ const TimeConfigSchema = z.object({
|
|||||||
maxLate: z
|
maxLate: z
|
||||||
.string()
|
.string()
|
||||||
.optional()
|
.optional()
|
||||||
.refine((duration) => {
|
.transform((duration, ctx) => {
|
||||||
if (duration == undefined) return true;
|
if (duration == undefined) return undefined;
|
||||||
const { hours, minutes } = parseTime(duration);
|
|
||||||
return hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59;
|
if (!duration.match(/^[0-9]{2}:[0-9]{2}$/)) {
|
||||||
}, 'Invalid duration format')
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.custom,
|
||||||
|
message: 'Duration must be in the format "HH:MM" e.g. "12:00"'
|
||||||
|
});
|
||||||
|
return z.NEVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsed = parseTime(duration);
|
||||||
|
|
||||||
|
if (!isValidTime(parsed)) {
|
||||||
|
ctx.addIssue({
|
||||||
|
code: z.ZodIssueCode.custom,
|
||||||
|
message:
|
||||||
|
'Invalid duration format. Max 23 hours, 59 minutes. Positives only.'
|
||||||
|
});
|
||||||
|
return z.NEVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsed;
|
||||||
|
})
|
||||||
});
|
});
|
||||||
type TimeConfig = z.infer<typeof TimeConfigSchema>;
|
type TimeConfig = z.infer<typeof TimeConfigSchema>;
|
||||||
|
|
||||||
@@ -56,16 +94,19 @@ const parseTime = (time: string): ParsedTime => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A predicate function to compare two times.
|
* A predicate function to compare two times.
|
||||||
* @param {ParsedTime} a A time to compare
|
* If {primary} is before {secondary}, returns -1.
|
||||||
* @param {ParsedTime} b A time to compare
|
* If {primary} is after {secondary}, returns 1.
|
||||||
* @returns {-1 | 0 | 1} The relative order of the two times in terms of -1, 0, or 1.
|
* If {primary} is equal to {secondary}, returns 0.
|
||||||
|
* @param {ParsedTime} primary A time to compare. The return value will be relative to this time.
|
||||||
|
* @param {ParsedTime} secondary A time to compare.
|
||||||
|
* @returns {-1 | 0 | 1} The relative order of {primary} in relation to {secondary} as a value of -1, 0, or 1.
|
||||||
*/
|
*/
|
||||||
function compareTime(a: ParsedTime, b: ParsedTime): -1 | 0 | 1 {
|
function compareTime(primary: ParsedTime, secondary: ParsedTime): -1 | 0 | 1 {
|
||||||
if (a.hours < b.hours) return -1;
|
if (primary.hours < secondary.hours) return -1;
|
||||||
if (a.hours > b.hours) return 1;
|
if (primary.hours > secondary.hours) return 1;
|
||||||
if (a.minutes < b.minutes) return -1;
|
if (primary.minutes < secondary.minutes) return -1;
|
||||||
if (a.minutes > b.minutes) return 1;
|
if (primary.minutes > secondary.minutes) return 1;
|
||||||
return 0;
|
return 0; // Identical
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -106,10 +147,7 @@ export const ConfigurationSchema = z.object({
|
|||||||
if (time.maxLate == undefined) return;
|
if (time.maxLate == undefined) return;
|
||||||
|
|
||||||
// Get the computed maxLate time.
|
// Get the computed maxLate time.
|
||||||
const maxLateTime = addTime(
|
const maxLateTime = addTime(time.time, time.maxLate!);
|
||||||
parseTime(time.time),
|
|
||||||
parseTime(time.maxLate!)
|
|
||||||
);
|
|
||||||
|
|
||||||
// If the computed maxLate time is invalid, add an issue.
|
// If the computed maxLate time is invalid, add an issue.
|
||||||
if (!isValidTime(maxLateTime))
|
if (!isValidTime(maxLateTime))
|
||||||
@@ -132,27 +170,22 @@ export const ConfigurationSchema = z.object({
|
|||||||
times.map((time, index) => ({ ...time, originalIndex: index }));
|
times.map((time, index) => ({ ...time, originalIndex: index }));
|
||||||
|
|
||||||
// Sort the values to make validation easier.
|
// Sort the values to make validation easier.
|
||||||
rememberedTimes.sort((a, b) =>
|
rememberedTimes.sort((a, b) => compareTime(a.time, b.time));
|
||||||
compareTime(parseTime(a.time), parseTime(b.time))
|
|
||||||
);
|
|
||||||
|
|
||||||
// Validate that no rules are overlapping from maxLate.
|
// Validate that no rules are overlapping from maxLate.
|
||||||
rememberedTimes.forEach((time, index) => {
|
rememberedTimes.forEach((time, index) => {
|
||||||
if (index === 0) return false;
|
if (index === 0) return false;
|
||||||
const previous = times[index - 1];
|
const previous = rememberedTimes[index - 1];
|
||||||
if (previous.maxLate == undefined) return false;
|
if (previous.maxLate == undefined) return false;
|
||||||
|
|
||||||
// If the days don't overlap, there's no need to check the time.
|
// If the days don't overlap, there's no need to check the time.
|
||||||
if (intersection(time.days, previous.days).size < 1) return false;
|
if (intersection(time.days, previous.days).size < 1) return false;
|
||||||
|
|
||||||
const previousEndTime = addTime(
|
const previousEndTime = addTime(previous.time, previous.maxLate);
|
||||||
parseTime(previous.time),
|
|
||||||
parseTime(previous.maxLate)
|
|
||||||
);
|
|
||||||
|
|
||||||
// If the previous rule's end time is greater than the current rule's start time, add an issue.
|
// If the previous rule's end time is greater than the current rule's start time, add an issue.
|
||||||
const startTime = parseTime(time.time);
|
// The times cannot overlap at all as multiple rules cannot be active at the same time.
|
||||||
if (compareTime(startTime, previousEndTime) < 1) {
|
if (compareTime(time.time, previousEndTime) <= 0) {
|
||||||
ctx.addIssue({
|
ctx.addIssue({
|
||||||
code: z.ZodIssueCode.custom,
|
code: z.ZodIssueCode.custom,
|
||||||
path: ['time', time.originalIndex],
|
path: ['time', time.originalIndex],
|
||||||
@@ -165,3 +198,44 @@ export const ConfigurationSchema = z.object({
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
export type Configuration = z.infer<typeof ConfigurationSchema>;
|
export type Configuration = z.infer<typeof ConfigurationSchema>;
|
||||||
|
|
||||||
|
const dayAsNumber: Record<string, DayEnum> = {
|
||||||
|
1: 'monday',
|
||||||
|
2: 'tuesday',
|
||||||
|
3: 'wednesday',
|
||||||
|
4: 'thursday',
|
||||||
|
5: 'friday',
|
||||||
|
6: 'saturday',
|
||||||
|
0: 'sunday'
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function getMatchingTime(
|
||||||
|
config: Configuration,
|
||||||
|
now = new Date()
|
||||||
|
): Promise<TimeConfig | null> {
|
||||||
|
const times = config.times.filter((time) => {
|
||||||
|
// If the day doesn't match, skip.
|
||||||
|
console.log(dayAsNumber[now.getDay().toString()]);
|
||||||
|
if (!time.days.has(dayAsNumber[now.getDay().toString()])) return false;
|
||||||
|
|
||||||
|
const startTime = time.time;
|
||||||
|
const endTime = addTime(
|
||||||
|
time.time,
|
||||||
|
time.maxLate ?? { hours: 0, minutes: 0 }
|
||||||
|
);
|
||||||
|
|
||||||
|
const nowTime = { hours: now.getHours(), minutes: now.getMinutes() };
|
||||||
|
|
||||||
|
return (
|
||||||
|
compareTime(nowTime, startTime) >= 0 && compareTime(nowTime, endTime) <= 0
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// This shouldn't be thrown, if I did my job right.
|
||||||
|
if (times.length > 1)
|
||||||
|
throw new Error(
|
||||||
|
"Multiple rules matched the current time. This shouldn't happen."
|
||||||
|
);
|
||||||
|
|
||||||
|
return times[0];
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user