From 9dc23cd458f1011366086bdb26e7693f8f30d2ee Mon Sep 17 00:00:00 2001 From: Simon Ilincev Date: Wed, 2 Oct 2024 17:48:55 -0400 Subject: [PATCH] chore: normalize search (#942) * chore: normalize search * feat: support longer searches by code *and* title --- .../Modals/NewCourse/CourseSelector.vue | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/components/Modals/NewCourse/CourseSelector.vue b/src/components/Modals/NewCourse/CourseSelector.vue index 05d7011a2..4c970c8cf 100644 --- a/src/components/Modals/NewCourse/CourseSelector.vue +++ b/src/components/Modals/NewCourse/CourseSelector.vue @@ -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); };