duebot-test / src /utils /stringFormatter.js
Ali00922's picture
Upload 12 files
c4be319 verified
raw
history blame contribute delete
997 Bytes
function formatTitle(title) {
if (!title) return 'Unknown Assignment';
// Remove newline characters and invisible spaces common in scraped text
title = title.replace(/[\r\n\t]+/g, ' ').trim();
if (title.startsWith('"') && title.endsWith('"')) {
title = title.substring(1, title.length - 1);
}
title = title.replace(/\s*is due$/i, '');
return title.trim();
}
function formatCourse(courseStr) {
if (!courseStr) return 'Unknown Course';
// Moodle course strings often come with the title injected first due to HTML structure
const lines = courseStr.split('\n').map(l => l.replace(/[\r\n\t]+/g, ' ').trim()).filter(l => l.length > 0);
let courseLine = lines.length >= 2 ? lines[1] : lines[0];
if (courseLine.startsWith('"') && courseLine.endsWith('"')) {
courseLine = courseLine.substring(1, courseLine.length - 1);
}
return courseLine.trim();
}
module.exports = { formatTitle, formatCourse };