Skip to content

Commit

Permalink
Merge pull request #501 from caramurphyy/cara-enable-search-by-number
Browse files Browse the repository at this point in the history
Improvement: Enable Searching For Courses By Course Number
  • Loading branch information
atcupps authored Oct 28, 2024
2 parents c1530c2 + 745f346 commit b783884
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
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

0 comments on commit b783884

Please sign in to comment.