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

Sort shortest term #28

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
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
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