Skip to content

Commit

Permalink
chore: normalize search (#942)
Browse files Browse the repository at this point in the history
* chore: normalize search

* feat: support longer searches by code *and* title
  • Loading branch information
Destaq authored Oct 2, 2024
1 parent b929b5e commit 9dc23cd
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/components/Modals/NewCourse/CourseSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,32 @@ const getMatchingCourses = (
/* code array for results that contain course code and title array for results that contain title */
const code: CornellCourseRosterCourse[] = [];
const title: CornellCourseRosterCourse[] = [];
const codeAndTitle: CornellCourseRosterCourse[] = [];
let filteredCourses: readonly CornellCourseRosterCourse[] = [];
if (coursesArray !== undefined) {
filteredCourses = coursesArray;
} else {
filteredCourses = filter != null ? fullCoursesArray.filter(filter) : fullCoursesArray;
}
const normalizedSearchText = searchText.toUpperCase().replace(/\s+/g, '').replace(/:/g, '');
for (const course of filteredCourses) {
const courseCode = `${course.subject} ${course.catalogNbr}`;
if (courseCode.toUpperCase().includes(searchText)) {
const courseCode = `${course.subject}${course.catalogNbr}`.toUpperCase().replace(/\s+/g, '');
const courseTitle = course.titleLong.toUpperCase().replace(/\s+/g, '');
if (courseCode.includes(normalizedSearchText)) {
code.push(course);
} else if (course.titleLong.toUpperCase().includes(searchText)) {
} else if (courseTitle.includes(normalizedSearchText)) {
title.push(course);
} else if ((courseCode + courseTitle).includes(normalizedSearchText)) {
codeAndTitle.push(course);
}
}
// Sort both results by title
// Sort all results by title, and prioritize code matches over other matches.
code.sort((first, second) => first.titleLong.localeCompare(second.titleLong));
title.sort((first, second) => first.titleLong.localeCompare(second.titleLong));
codeAndTitle.sort((first, second) => first.titleLong.localeCompare(second.titleLong));
/* prioritize code matches over title matches */
return code.concat(title);
return code.concat(title).concat(codeAndTitle);
// limit the number of results to 10
// return code.concat(title).slice(0, 10);
};
Expand Down

0 comments on commit 9dc23cd

Please sign in to comment.