From 2492dfdd33f09e05d02d7c37aa4a36d4ada6ed53 Mon Sep 17 00:00:00 2001 From: aravk Date: Mon, 3 Apr 2023 21:36:01 -0500 Subject: [PATCH 1/3] Read poorly parsed courses with regex --- src/server/trpc/router/validator.ts | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/server/trpc/router/validator.ts b/src/server/trpc/router/validator.ts index 70232cb76..9ad71c10a 100644 --- a/src/server/trpc/router/validator.ts +++ b/src/server/trpc/router/validator.ts @@ -77,6 +77,9 @@ export const validatorRouter = router({ const coReqHash = new Map, number]>>(); const preReqHash = new Map, number]>>(); const coOrPreReqHash = new Map, number]>>(); + // Regex to parse course from description of improperly parsed course + const re = /\b[A-Z]{2} \d{4}\b/; + /* Recursive function to check for prereqs. * TODO: Move to a client side function. Possibly a hook. */ @@ -105,8 +108,10 @@ export const validatorRouter = router({ } const temp: [Array, number] = [[], 0]; for (const option of requirements.options) { - if (option.type === 'course') { - const course = courseMapWithIdKey.get(option.class_reference); + if (option.type === 'course' || option.type === 'other') { + // 'other' might be an improperly parsed course + // if it's not, `course` will be set to undefined so nothing will happen + const course = option.type === 'course' ? courseMapWithIdKey.get(option.class_reference) : option.description.match(re)?.[0]; if (course) { const data = courseHash.get(course as string); if (data === undefined) { @@ -124,8 +129,6 @@ export const validatorRouter = router({ } else { count++; } - } else if (option.type === 'other') { - // count++; } } @@ -150,15 +153,17 @@ export const validatorRouter = router({ } const temp: [Array, number] = [[], 0]; for (const option of requirements.options) { - if (option.type === 'course') { - const course = courseMapWithIdKey.get(option.class_reference); + if (option.type === 'course' || option.type === 'other') { + // 'other' might be an improperly parsed course + // if it's not, `course` will be set to undefined so nothing will happen + const course = option.type === 'course' ? courseMapWithIdKey.get(option.class_reference) : option.description.match(re)?.[0]; if (course) { const data = courseHash.get(course as string); if (data === undefined) { temp[0].push(course as string); continue; } - if (data <= semester) { + if (data === semester) { count++; } else { temp[0].push(course as string); @@ -171,8 +176,6 @@ export const validatorRouter = router({ } else { count++; } - } else if (option.type === 'other') { - // count++; } } if (count >= requirements.required) { @@ -196,8 +199,10 @@ export const validatorRouter = router({ } const temp: [Array, number] = [[], 0]; for (const option of requirements.options) { - if (option.type === 'course') { - const course = courseMapWithIdKey.get(option.class_reference); + if (option.type === 'course' || option.type === 'other') { + // 'other' might be an improperly parsed course + // if it's not, `course` will be set to undefined so nothing will happen + const course = option.type === 'course' ? courseMapWithIdKey.get(option.class_reference) : option.description.match(re)?.[0]; if (course) { const data = courseHash.get(course as string); if (data === undefined) { @@ -217,8 +222,6 @@ export const validatorRouter = router({ } else { count++; } - } else if (option.type === 'other') { - // count++; } } if (count >= requirements.required) { From ed02f89e5acfe03f3406ea2c5ef132205de15408 Mon Sep 17 00:00:00 2001 From: aravk Date: Mon, 3 Apr 2023 21:36:23 -0500 Subject: [PATCH 2/3] Run prettier --- src/server/trpc/router/validator.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/server/trpc/router/validator.ts b/src/server/trpc/router/validator.ts index 9ad71c10a..6cf53ff0b 100644 --- a/src/server/trpc/router/validator.ts +++ b/src/server/trpc/router/validator.ts @@ -111,7 +111,10 @@ export const validatorRouter = router({ if (option.type === 'course' || option.type === 'other') { // 'other' might be an improperly parsed course // if it's not, `course` will be set to undefined so nothing will happen - const course = option.type === 'course' ? courseMapWithIdKey.get(option.class_reference) : option.description.match(re)?.[0]; + const course = + option.type === 'course' + ? courseMapWithIdKey.get(option.class_reference) + : option.description.match(re)?.[0]; if (course) { const data = courseHash.get(course as string); if (data === undefined) { @@ -156,14 +159,17 @@ export const validatorRouter = router({ if (option.type === 'course' || option.type === 'other') { // 'other' might be an improperly parsed course // if it's not, `course` will be set to undefined so nothing will happen - const course = option.type === 'course' ? courseMapWithIdKey.get(option.class_reference) : option.description.match(re)?.[0]; + const course = + option.type === 'course' + ? courseMapWithIdKey.get(option.class_reference) + : option.description.match(re)?.[0]; if (course) { const data = courseHash.get(course as string); if (data === undefined) { temp[0].push(course as string); continue; } - if (data === semester) { + if (data <= semester) { count++; } else { temp[0].push(course as string); @@ -202,7 +208,10 @@ export const validatorRouter = router({ if (option.type === 'course' || option.type === 'other') { // 'other' might be an improperly parsed course // if it's not, `course` will be set to undefined so nothing will happen - const course = option.type === 'course' ? courseMapWithIdKey.get(option.class_reference) : option.description.match(re)?.[0]; + const course = + option.type === 'course' + ? courseMapWithIdKey.get(option.class_reference) + : option.description.match(re)?.[0]; if (course) { const data = courseHash.get(course as string); if (data === undefined) { From 131c8080dab5a2d2a309e748c2a508ebaf59c64b Mon Sep 17 00:00:00 2001 From: aravk Date: Tue, 4 Apr 2023 15:03:30 -0500 Subject: [PATCH 3/3] Match 'other' courses with 2-4 chars --- src/server/trpc/router/validator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/trpc/router/validator.ts b/src/server/trpc/router/validator.ts index 6cf53ff0b..b6a457bdd 100644 --- a/src/server/trpc/router/validator.ts +++ b/src/server/trpc/router/validator.ts @@ -78,7 +78,7 @@ export const validatorRouter = router({ const preReqHash = new Map, number]>>(); const coOrPreReqHash = new Map, number]>>(); // Regex to parse course from description of improperly parsed course - const re = /\b[A-Z]{2} \d{4}\b/; + const re = /\b[A-Z]{2,4} \d{4}\b/; /* Recursive function to check for prereqs. * TODO: Move to a client side function. Possibly a hook.