Skip to content

Commit

Permalink
Sort shortest term (#28)
Browse files Browse the repository at this point in the history
* feat: add sorting by shortest term

* test: write tests
  • Loading branch information
KevinWu098 authored Dec 27, 2023
1 parent 159dcbb commit 4d643ee
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
49 changes: 45 additions & 4 deletions __tests__/search-filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const data = {
cvcId: "123456",
niceToHaves: ["Zero Textbook Cost"],
units: 4,
term: "Mar 11 - May 25",
term: "January 1 - May 31",
startMonth: 1,
startDay: 1,
endMonth: 6,
endDay: 1,
endMonth: 5,
endDay: 31,
tuition: 138,
async: true,
hasOpenSeats: true,
Expand All @@ -38,7 +38,7 @@ const data = {
cvcId: "1234567",
niceToHaves: ["Zero Textbook Cost"],
units: 16,
term: "Mar 11 - May 25",
term: "January 1 - June 1",
startMonth: 1,
startDay: 1,
endMonth: 6,
Expand All @@ -52,6 +52,27 @@ const data = {
articulatesTo: ["placeholder course 2"],
fulfillsGEs: ["II"],
},
{
sendingInstitution: "placeholder sending institution 3",
courseCode: "placeholder course code 3",
courseName: "placeholder course name 3",
cvcId: "1234567",
niceToHaves: ["Zero Textbook Cost"],
units: 16,
term: "January 1 - June 1",
startMonth: 12,
startDay: 30,
endMonth: 1,
endDay: 1,
tuition: 100,
async: false,
hasOpenSeats: false,
hasPrereqs: false,
instantEnrollment: false,
assistPath: "placeholder path 3",
articulatesTo: ["placeholder course 3"],
fulfillsGEs: ["II"],
},
],
};

Expand Down Expand Up @@ -242,4 +263,24 @@ describe("Search Sorting", () => {
"placeholder sending institution 2",
);
});

test("shortest term sorts correctly", async () => {
const result = filterData(data.courses.slice(0, 2), {
...defaultFilterValues,
sort: "Shortest Term",
});
expect(result[0].sendingInstitution).toEqual(
"placeholder sending institution 1",
);
});

test("shortest term sorts correctly on december courses", async () => {
const result = filterData(data.courses, {
...defaultFilterValues,
sort: "Shortest Term",
});
expect(result[0].sendingInstitution).toEqual(
"placeholder sending institution 3",
);
});
});
16 changes: 15 additions & 1 deletion components/search/filterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,21 @@ export function filterData(data: CollegeObject[], filterValues: FilterValues) {
? filteredResults.sort((courseA, courseB) => {
return courseA.tuition - courseB.tuition;
})
: filteredResults;
: filterValues.sort == "Shortest Term"
? filteredResults.sort((courseA, courseB) => {
const termLengthA =
((courseA.endMonth - courseA.startMonth + 12) % 12) *
30 +
(courseA.endDay - courseA.startDay);

const termLengthB =
((courseB.endMonth - courseB.startMonth + 12) % 12) *
30 +
(courseB.endDay - courseB.startDay);

return termLengthA - termLengthB;
})
: filteredResults;

return sortedResults;
}
1 change: 1 addition & 0 deletions components/search/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ const Search = () => {
"Default Sort",
"Alphabetical",
"Tuition",
"Shortest Term",
]}
onChange={setSort}
/>
Expand Down

0 comments on commit 4d643ee

Please sign in to comment.