diff --git a/site/src/components/course-planner/course-search/CourseSearch.svelte b/site/src/components/course-planner/course-search/CourseSearch.svelte index a34257e..26942bf 100644 --- a/site/src/components/course-planner/course-search/CourseSearch.svelte +++ b/site/src/components/course-planner/course-search/CourseSearch.svelte @@ -26,6 +26,7 @@ Copyright (C) 2024 Andrew Cupps // Load profs and depts data export let data; let depts: Department[] = data.departments; + let deptList = depts.map((d) => d.name); // Create course lookup table const courseLookup = getCourseLookup(depts); @@ -36,7 +37,7 @@ Copyright (C) 2024 Andrew Cupps function handleInput() { // Sorting is done to ensure courses are displayed in // alphabetical order - searchResults = searchCourses(searchInput, courseLookup) + searchResults = searchCourses(searchInput, courseLookup, deptList) .sort((a, b) => { return a.code.localeCompare(b.code); }); diff --git a/site/src/lib/course-planner/CourseSearch.ts b/site/src/lib/course-planner/CourseSearch.ts index c8408fa..099c10b 100644 --- a/site/src/lib/course-planner/CourseSearch.ts +++ b/site/src/lib/course-planner/CourseSearch.ts @@ -40,11 +40,12 @@ export function getCourseLookup(departments: Department[]): * @param input A string of input used to search for `Course`s * @param courseLookup A `Record` used to match departments and course numbers * to `Course`s; can be generated using `getCourseLookup` + * @param deptList A list of departments used to search for courses by number * * @returns An array of possible courses given the `input`. */ export function searchCourses(input: string, courseLookup: - Record>): Course[] { + Record>, deptList: string[]): Course[] { const result: Course[] = []; // For an `input` to be worth searching, it should be at least the four // letter department code, and the department code must be in @@ -72,6 +73,39 @@ export function searchCourses(input: string, courseLookup: } } } + } + + // If the search input is just numbers, match courses with that number + if (simpleInput.length >= 2 && /^[0-9]+$/i.test(simpleInput)) { + + // get all the courses from every department + const allDeptCourses: Record = {}; + for (const dept of deptList) { + const deptCourses = courseLookup[dept]; + if (deptCourses !== undefined) { + for (const courseCode in deptCourses) { + const uniqueCourseCode = `${dept}-${courseCode}`; + allDeptCourses[uniqueCourseCode] = deptCourses[courseCode]; + } + } + } + + for (const courseCode in allDeptCourses) { + let shouldBeInResult = true; + if (simpleInput.length > courseCode.length) { + shouldBeInResult = false; + } else { + const courseNumber = courseCode.substring(5); + for (let i = 0; i < simpleInput.length; i++) { + if (simpleInput[i] != courseNumber[i]) { + shouldBeInResult = false; + } + } + } + if (shouldBeInResult) { + result.push(allDeptCourses[courseCode]); + } + } } return result;