1
0

feat: Implemented plus notation
All checks were successful
Remote Deploy / deploy (push) Successful in 3s

This commit is contained in:
2025-09-02 08:13:49 +02:00
parent 156be69826
commit 3a8b2dcbc3

View File

@@ -2,30 +2,72 @@ export default function parseAbsence(str, teacherMap) {
str = str.trim().replace(/\s+/g, " "); str = str.trim().replace(/\s+/g, " ");
const result = { const result = {
teacher: null, // String or null teacher: null,
teacherCode: null, // String or null teacherCode: null,
type: null, // "wholeDay" | "range" | "single" | "invalid" type: null, // "wholeDay" | "range" | "single" | "invalid"
hours: null // { from: number, to: number } or number hours: null
}; };
// Regex patterns (with flexible spaces) // Patterns (teacher can be before or after the hours)
const wholeDayPattern = /^([A-Za-z]+)$/; const wholeDayPattern = /^([A-Za-z]+)$/;
const rangePattern = /^(\d+)\s*-\s*(\d+)\s+([A-Za-z]+)$/; const rangePattern = /^(\d+)\s*-\s*(\d+)\s+([A-Za-z]+)$|^([A-Za-z]+)\s+(\d+)\s*-\s*(\d+)$/;
const singleHourPattern = /^(\d+)\s+([A-Za-z]+)$/; const singleHourPattern = /^(\d+)\s+([A-Za-z]+)$|^([A-Za-z]+)\s+(\d+)$/;
const plusPattern = /^(\d+)\+\s+([A-Za-z]+)$|^([A-Za-z]+)\s+(\d+)\+$/;
if (rangePattern.test(str)) { if (rangePattern.test(str)) {
const [, from, to, teacherCode] = str.match(rangePattern); const match = str.match(rangePattern);
result.teacher = teacherMap[teacherCode.toLowerCase()]; // Case 1: hours first, then teacher
if (match[1] && match[2] && match[3]) {
const [, from, to, teacherCode] = match;
result.teacherCode = teacherCode; result.teacherCode = teacherCode;
result.teacher = teacherMap[teacherCode.toLowerCase()];
result.type = "range"; result.type = "range";
result.hours = { from: parseInt(from), to: parseInt(to) }; result.hours = { from: parseInt(from), to: parseInt(to) };
} }
else if (singleHourPattern.test(str)) { // Case 2: teacher first, then hours
const [, hour, teacherCode] = str.match(singleHourPattern); else {
result.teacher = teacherMap[teacherCode.toLowerCase()]; const [, , , teacherCode, from, to] = match;
result.teacherCode = teacherCode; result.teacherCode = teacherCode;
result.teacher = teacherMap[teacherCode.toLowerCase()];
result.type = "range";
result.hours = { from: parseInt(from), to: parseInt(to) };
}
}
else if (plusPattern.test(str)) {
const match = str.match(plusPattern);
if (match[1] && match[2]) {
// hours first
const [, from, teacherCode] = match;
result.teacherCode = teacherCode;
result.teacher = teacherMap[teacherCode.toLowerCase()];
result.type = "range";
result.hours = { from: parseInt(from), to: 10 };
} else {
// teacher first
const [, , teacherCode, from] = match;
result.teacherCode = teacherCode;
result.teacher = teacherMap[teacherCode.toLowerCase()];
result.type = "range";
result.hours = { from: parseInt(from), to: 10 };
}
}
else if (singleHourPattern.test(str)) {
const match = str.match(singleHourPattern);
if (match[1] && match[2]) {
// hours first
const [, hour, teacherCode] = match;
result.teacherCode = teacherCode;
result.teacher = teacherMap[teacherCode.toLowerCase()];
result.type = "single"; result.type = "single";
result.hours = parseInt(hour); result.hours = parseInt(hour);
} else {
// teacher first
const [, , teacherCode, hour] = match;
result.teacherCode = teacherCode;
result.teacher = teacherMap[teacherCode.toLowerCase()];
result.type = "single";
result.hours = parseInt(hour);
}
} }
else if (wholeDayPattern.test(str)) { else if (wholeDayPattern.test(str)) {
const [, teacherCode] = str.match(wholeDayPattern); const [, teacherCode] = str.match(wholeDayPattern);