Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: Enable Searching For Courses By Course Number #501

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
Expand Down
36 changes: 35 additions & 1 deletion site/src/lib/course-planner/CourseSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Record<string, Course>>): Course[] {
Record<string, Record<string, Course>>, 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
Expand Down Expand Up @@ -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<string, Course> = {};
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;
Expand Down
Loading