-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutilities.ts
More file actions
70 lines (64 loc) · 2.52 KB
/
utilities.ts
File metadata and controls
70 lines (64 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {
TCourseId,
TPayloadCourses,
TPayloadCoursesDataDynamic,
TPayloadCoursesDataStatic,
} from '@/lib/types';
import { DOMAIN_GATECH, DOMAIN_OUTLOOK } from '@/lib/constants';
/* --- MAPPERS --- */
/**
* Merges dynamic course stats with static course data to create full Course model
* @param coursesDataDynamic Dynamic stats (object keyed by courseId)
* @param coursesDataStatic Static course data fetched from data repo
* @returns Complete `Course` data model as `TPayloadCourses`
*/
export const mapDynamicCoursesDataToCourses = (
coursesDataDynamic: TPayloadCoursesDataDynamic,
coursesDataStatic: TPayloadCoursesDataStatic
) => {
const courses = {} as TPayloadCourses;
// Include ALL courses from static data repo, add dynamic stats where available
(Object.keys(coursesDataStatic) as TCourseId[])
.filter((courseId) => !coursesDataStatic[courseId]?.isDeprecated)
.forEach((courseId) => {
const staticData = coursesDataStatic[courseId];
const dynamicData = coursesDataDynamic[courseId];
courses[courseId] = {
...staticData,
courseId,
numReviews: dynamicData?.numReviews ?? 0,
avgWorkload: dynamicData?.avgWorkload ?? null,
avgDifficulty: dynamicData?.avgDifficulty ?? null,
avgOverall: dynamicData?.avgOverall ?? null,
avgStaffSupport: dynamicData?.avgStaffSupport ?? null,
reviewsCountsByYearSem: dynamicData?.reviewsCountsByYearSem ?? {},
};
});
return courses;
};
/* --- UTILITY FUNCTIONS --- */
/**
* Extract email domain from email input
* @param userEmail Input user email
* @param domain Input email domain (including prefix `@`)
* @returns Email domain
* @example
* // `gatechDomain` has value `@gatech.edu`
* const gatechDomain = extractEmailDomain('gpb@gatech.edu', '@gatech.edu');
*/
export const extractEmailDomain = (userEmail: string, domain: string) =>
userEmail.toLowerCase().slice(userEmail.length - domain.length);
/**
* Determine if `userEmail` has domain `@gatech.edu`
* @param userEmail Input user email
* @returns Boolean `true` (domain is `@gatech.edu`) or `false` (otherwise)
*/
export const isGTEmail = (userEmail: string) =>
extractEmailDomain(userEmail, DOMAIN_GATECH).includes(DOMAIN_GATECH);
/**
* Determine if `userEmail` has domain `@outlook.com`
* @param userEmail Input user email
* @returns Boolean `true` (domain is `@outlook.com`) or `false` (otherwise)
*/
export const isOutlookEmail = (userEmail: string) =>
extractEmailDomain(userEmail, DOMAIN_OUTLOOK).includes(DOMAIN_OUTLOOK);