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

Commit

Permalink
perf: ⚡️ use switch-case instead of if-else
Browse files Browse the repository at this point in the history
  • Loading branch information
ecxyzzy committed Dec 18, 2023
1 parent 49215ae commit eb35bd9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
21 changes: 11 additions & 10 deletions apps/api/src/routes/v1/rest/courses/{id}/+endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { courses } from "virtual:courses";
export const GET = createHandler(async (event, context, res) => {
const headers = event.headers;
const requestId = context.awsRequestId;
const params = event.pathParameters;
const { id } = event.pathParameters ?? {};

if (params?.id == null) {
return res.createErrorResult(400, "Course number not provided", requestId);
switch (id) {
case null:
case undefined:
return res.createErrorResult(400, "Course number not provided", requestId);
case "all":
return res.createOKResult(Object.values(courses), headers, requestId);
default:
return courses[decodeURIComponent(id)]
? res.createOKResult(courses[decodeURIComponent(id)], headers, requestId)
: res.createErrorResult(404, `Course ${id} not found`, requestId);
}
if (params?.id === "all") {
return res.createOKResult(Object.values(courses), headers, requestId);
}
if (courses[decodeURIComponent(params.id)]) {
return res.createOKResult(courses[decodeURIComponent(params.id)], headers, requestId);
}
return res.createErrorResult(404, `Course ${params.id} not found`, requestId);
});
21 changes: 11 additions & 10 deletions apps/api/src/routes/v1/rest/instructors/{id}/+endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { instructors } from "virtual:instructors";
export const GET = createHandler(async (event, context, res) => {
const headers = event.headers;
const requestId = context.awsRequestId;
const params = event.pathParameters;
const { id } = event.pathParameters ?? {};

if (params?.id == null) {
return res.createErrorResult(400, "Instructor UCInetID not provided", requestId);
switch (id) {
case null:
case undefined:
return res.createErrorResult(400, "Instructor UCInetID not provided", requestId);
case "all":
return res.createOKResult(Object.values(instructors), headers, requestId);
default:
return instructors[decodeURIComponent(id)]
? res.createOKResult(instructors[decodeURIComponent(id)], headers, requestId)
: res.createErrorResult(404, `Instructor ${id} not found`, requestId);
}
if (params.id === "all") {
return res.createOKResult(Object.values(instructors), headers, requestId);
}
if (instructors[decodeURIComponent(params.id)]) {
return res.createOKResult(instructors[decodeURIComponent(params.id)], headers, requestId);
}
return res.createErrorResult(404, `Instructor ${params.id} not found`, requestId);
});

0 comments on commit eb35bd9

Please sign in to comment.