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

Reinstate Search with Exact Course Code and Title Match without Fuse.js #494

Merged
merged 1 commit into from
May 5, 2021
Merged
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
59 changes: 26 additions & 33 deletions src/components/Modals/NewCourse/CourseSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,34 @@

<script lang="ts">
import { PropType, defineComponent } from 'vue';
import Fuse from 'fuse.js';
import { fullCoursesArray } from '@/assets/courses/typed-full-courses';

interface SearchableCourse extends CornellCourseRosterCourse {
courseCode?: string;
fullCourseString?: string;
}
const getMatchingCourses = (
searchText: string,
filter?: (course: CornellCourseRosterCourse) => boolean
): readonly CornellCourseRosterCourse[] => {
// search after value length of 2 to reduce search times of courses
if (!searchText || searchText.length < 2) return [];
/* code array for results that contain course code and title array for results that contain title */
const code: CornellCourseRosterCourse[] = [];
const title: CornellCourseRosterCourse[] = [];
const filteredCourses = filter != null ? fullCoursesArray.filter(filter) : fullCoursesArray;
for (const course of filteredCourses) {
const courseCode = `${course.subject} ${course.catalogNbr}`;
if (courseCode.toUpperCase().includes(searchText)) {
code.push(course);
} else if (course.titleLong.toUpperCase().includes(searchText)) {
title.push(course);
}
}
// Sort both results by title
code.sort((first, second) => first.titleLong.localeCompare(second.titleLong));
title.sort((first, second) => first.titleLong.localeCompare(second.titleLong));

const fullSearchableCoursesArray = fullCoursesArray.map((course: SearchableCourse) => {
course.courseCode = `${course.subject} ${course.catalogNbr}`;
course.fullCourseString = `${course.subject} ${course.catalogNbr} ${course.titleLong}`;
return course;
});
/* prioritize code matches over title matches */
// limit the number of results to 10
return code.concat(title).slice(0, 10);
};

export default defineComponent({
props: {
Expand All @@ -63,29 +78,7 @@ export default defineComponent({
},
computed: {
matches(): readonly CornellCourseRosterCourse[] {
if (!this.searchText || this.searchText.length < 2) return [];
const options = {
keys: [
{ name: 'courseCode', weight: 2 },
{ name: 'subject', weight: 3 },
{ name: 'fullCourseString', weight: 2.5 },
{ name: 'catalogNbr', weight: 1.5 },
{ name: 'titleLong', weight: 1 },
],
};

const fuse = new Fuse(this.searchableCourses, options);
const result: readonly SearchableCourse[] = fuse
.search(this.searchText, { limit: 10 })
.map(elem => elem.item);
return result;
},
searchableCourses(): readonly SearchableCourse[] {
const courses: readonly SearchableCourse[] =
this.courseFilter != null
? fullSearchableCoursesArray.filter(this.courseFilter)
: fullSearchableCoursesArray;
return courses;
return getMatchingCourses(this.searchText.toUpperCase(), this.courseFilter);
},
},
mounted() {
Expand Down