1
0

feat: Absences
All checks were successful
Remote Deploy / deploy (push) Successful in 4s

This commit is contained in:
2025-08-31 16:35:22 +02:00
parent fc8d4d3545
commit 4985d574d5
6 changed files with 374 additions and 26 deletions

View File

@@ -0,0 +1,44 @@
function parseAbsence(str, teacherMap) {
str = str.trim().replace(/\s+/g, " ");
const result = {
teacher: null, // String or null
teacherCode: null, // String or null
type: null, // "wholeDay" | "range" | "single" | "invalid"
hours: null // { from: number, to: number } or number
};
// Regex patterns (with flexible spaces)
const wholeDayPattern = /^([A-Za-z]+)$/;
const rangePattern = /^(\d+)\s*-\s*(\d+)\s+([A-Za-z]+)$/;
const singleHourPattern = /^(\d+)\s+([A-Za-z]+)$/;
if (rangePattern.test(str)) {
const [, from, to, teacherCode] = str.match(rangePattern);
result.teacher = teacherMap[teacherCode.toLowerCase()];
result.teacherCode = teacherCode;
result.type = "range";
result.hours = { from: parseInt(from), to: parseInt(to) };
}
else if (singleHourPattern.test(str)) {
const [, hour, teacherCode] = str.match(singleHourPattern);
result.teacher = teacherMap[teacherCode.toLowerCase()];
result.teacherCode = teacherCode;
result.type = "single";
result.hours = parseInt(hour);
}
else if (wholeDayPattern.test(str)) {
const [, teacherCode] = str.match(wholeDayPattern);
result.teacher = teacherMap[teacherCode.toLowerCase()];
result.teacherCode = teacherCode;
result.type = "wholeDay";
}
else {
result.type = "invalid";
result.original = str;
}
return result;
}
module.exports = parseAbsence;