Skip to content

Commit

Permalink
Optimize the automatic subject detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Aug 27, 2024
1 parent 1df3fc1 commit f51fefe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/settings/timetable/daysUntil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { describe, expect, it } from 'vitest';
const EXAMPLE_TIMETABLE: Timetable = {
mon: ['German', 'Religion', 'Math'],
tue: ['English', 'P.E.', 'Music'],
wed: ['English', 'Math', 'Physics'],
wed: ['English', 'mathemathics', 'Physics'],
thu: ['Art', 'Biology', 'German'],
fri: ['History', 'Geography', 'English'],
sat: [],
Expand Down
40 changes: 29 additions & 11 deletions src/lib/components/settings/timetable/daysUntil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import type { Timetable, TimetableWeekday } from '$lib/components/settings/timetable/types';
import { WEEKDAYS } from '$lib/components/settings/timetable/weekdays';
import { objectEntries } from '$lib/utils/objects/entries';

const serialized = {
informatik: ['informatik', 'info', 'computer science', 'programming'],
biologie: ['bio', 'biology', 'biologie'],
geschichte: ['geschichte', 'geschi'],
mathematik: ['mathe', 'mathematick', 'mathematik', 'mathemathik', 'mathemathics', 'math'],
religion: ['reli', 'religion'],
powi: ['powi', 'wipo'],
physics: ['physics', 'physic', 'physik']
};

function timetableSerialize(subj: string) {
const subject = subj.trim().toLowerCase();
const entries = objectEntries(serialized);
const entry = entries.find(([_, alts]) => alts.includes(subj));

return entry ? entry[0] : subject;
}

export interface DaysUntilProps {
subject: string;
Expand All @@ -11,32 +30,31 @@ function privateDaysUntil({ subject, timetable, currentDay }: DaysUntilProps, co
const currentWeekdayIndex = WEEKDAYS.indexOf(currentDay);
const nextDay = WEEKDAYS[(currentWeekdayIndex + 1) % 7];

const isTommorow = timetable[nextDay].indexOf(subject) !== -1;
const isTommorow = timetable[nextDay].some((iterSubj) => {
if (!iterSubj) return false;
return timetableSerialize(iterSubj) === timetableSerialize(subject);
});

if (isTommorow) return count + 1;

if (count > 7) {
throw new Error('Infinite loop detected in timetable calculation');
}

return privateDaysUntil(
{
subject,
timetable,
currentDay: nextDay
},
count + 1
);
return privateDaysUntil({ subject, timetable, currentDay: nextDay }, count + 1);
}

/**
* Calculates the number of days until a given subject appears in the timetable.
*
* @param props - The properties object.
* @param props.subject - The subject to search for in the timetable.
* @param props.timetable - The timetable object containing subjects for each weekday.
* @param props.timetable - The timetable object containing subjects for each
* weekday.
* @param props.currentDay - The current day of the week.
* @returns The number of days until the subject appears next in the timetable.
* @throws {Error} Throws an error if an infinite loop is detected in the timetable calculation.
* @throws {Error} Throws an error if an infinite loop is detected in the
* timetable calculation.
*/
export function daysUntil(props: DaysUntilProps) {
return privateDaysUntil(props);
Expand Down
6 changes: 2 additions & 4 deletions src/lib/utils/dlool/smartSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const exactPairs = [
'Spanisch',
'Kunst',
'Französisch',
'Englisch',

'English',
'Spanish',
'French',
'German'
Expand Down Expand Up @@ -51,10 +53,6 @@ const rules: SmartDetectRule[] = [
subject: 'Biologie',
pattern: /^bio/
},
{
subject: 'Religion',
pattern: /^reli/
},
{
subject: 'Religion',
pattern: /^reli/
Expand Down
10 changes: 3 additions & 7 deletions src/lib/utils/icons/subjectIcons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
BuildingLibrary,
Calculator,
ComputerDesktop,
HomeModern,
Fire,
Language,
LightBulb,
MusicalNote,
Expand All @@ -29,11 +29,7 @@ const iconObjs: IconObj[] = [
icon: BugAnt
},
{
subjects: ['biologie', 'bio', 'biology'],
icon: Beaker
},
{
subjects: ['biologie', 'bio', 'biology'],
subjects: ['chemie', 'chemistry'],
icon: Beaker
},
{
Expand Down Expand Up @@ -78,7 +74,7 @@ const iconObjs: IconObj[] = [
},
{
subjects: ['sport', 'pe'],
icon: HomeModern
icon: Fire
},
{
subjects: ['powi', 'politik', 'wirtschaft', 'economy'],
Expand Down

0 comments on commit f51fefe

Please sign in to comment.