Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
perf: ⚡️ use LUTs instead of switch-case
Browse files Browse the repository at this point in the history
  • Loading branch information
ecxyzzy committed Dec 21, 2023
1 parent 74998ee commit 3c1933a
Showing 1 changed file with 23 additions and 41 deletions.
64 changes: 23 additions & 41 deletions apps/api/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import { Course as PrismaCourse, CourseLevel as PrismaCourseLevel } from "@libs/db";
import { Course, CourseLevel, GE, GECategory, PrerequisiteTree } from "@peterportal-api/types";
import { Course as PrismaCourse } from "@libs/db";
import { Course, CourseLevel, GECategory, PrerequisiteTree } from "@peterportal-api/types";

const days = ["Su", "M", "Tu", "W", "Th", "F", "Sa"];

const courseLevels: Record<string, CourseLevel> = {
LowerDiv: "Lower Division (1-99)",
UpperDiv: "Upper Division (100-199)",
Graduate: "Graduate/Professional Only (200+)",
};

const geMapping: Record<string, GECategory> = {
"GE-1A": "GE Ia: Lower Division Writing",
"GE-1B": "GE Ib: Upper Division Writing",
"GE-2": "GE II: Science and Technology",
"GE-3": "GE III: Social & Behavioral Sciences",
"GE-4": "GE IV: Arts and Humanities",
"GE-5A": "GE Va: Quantitative Literacy",
"GE-5B": "GE Vb: Formal Reasoning",
"GE-6": "GE VI: Language Other Than English",
"GE-7": "GE VII: Multicultural Studies",
"GE-8": "GE VIII: International/Global Issues",
};

/**
* Input to a transform function.
*/
Expand Down Expand Up @@ -40,45 +59,8 @@ export const flattenDayStringsAndSplit = (value: TransformInput): TransformOutpu
: undefined;

export function normalizeCourse(course: PrismaCourse): Course {
let courseLevel: CourseLevel;
switch (course.courseLevel as PrismaCourseLevel) {
case "LowerDiv":
courseLevel = "Lower Division (1-99)";
break;
case "UpperDiv":
courseLevel = "Upper Division (100-199)";
break;
case "Graduate":
courseLevel = "Graduate/Professional Only (200+)";
break;
}
const geList = (course.geList as Array<Omit<GE, "ANY">>).map((x): GECategory => {
switch (x) {
case "GE-1A":
return "GE Ia: Lower Division Writing";
case "GE-1B":
return "GE Ib: Upper Division Writing";
case "GE-2":
return "GE II: Science and Technology";
case "GE-3":
return "GE III: Social & Behavioral Sciences";
case "GE-4":
return "GE IV: Arts and Humanities";
case "GE-5A":
return "GE Va: Quantitative Literacy";
case "GE-5B":
return "GE Vb: Formal Reasoning";
case "GE-6":
return "GE VI: Language Other Than English";
case "GE-7":
return "GE VII: Multicultural Studies";
case "GE-8":
return "GE VIII: International/Global Issues";
// this branch should never happen
default:
throw new Error();
}
});
const courseLevel = courseLevels[course.courseLevel];
const geList = (course.geList as string[]).map((x) => geMapping[x]);
return {
...course,
courseLevel,
Expand Down

0 comments on commit 3c1933a

Please sign in to comment.