properly generating and formating instructor text

This commit is contained in:
Sriram Hariharan
2023-03-06 00:10:03 -06:00
parent 7401138d87
commit 8b5fabce0c
2 changed files with 45 additions and 14 deletions

View File

@@ -76,27 +76,40 @@ export class Course {
Object.assign(this, course); Object.assign(this, course);
} }
getInstructorString(options: { /**
/** The maximum number of instructors to show */ * Get a string representation of the instructors for this course
max?: number; * @param options - the options for how to format the instructor string
format: 'abbr' | 'first_last' | 'last' | 'full_name'; * @returns
}): string { */
const { max = 3, format } = options; getInstructorString(options: InstructorFormatOptions): string {
const { max = 3, format, prefix = '' } = options;
if (!this.instructors) { if (!this.instructors.length) {
return 'Undecided'; return `${prefix} Undecided`;
} }
const instructors = this.instructors.slice(0, max); const instructors = this.instructors.slice(0, max);
switch (format) { switch (format) {
case 'abbr': case 'abbr':
return instructors.map(instructor => `${instructor.firstName?.[0]}. ${instructor.lastName}`).join(', '); return (
prefix +
instructors
.map(instructor => {
let firstInitial = instructor.firstName?.[0];
if (firstInitial) {
firstInitial += '. ';
}
return `${firstInitial}${instructor.lastName}`;
})
.join(', ')
);
case 'full_name': case 'full_name':
return instructors.map(instructor => instructor.fullName).join(', '); return prefix + instructors.map(instructor => instructor.fullName).join(', ');
case 'first_last': case 'first_last':
return instructors.map(instructor => `${instructor.firstName} ${instructor.lastName}`).join(', '); return (
prefix + instructors.map(instructor => `${instructor.firstName} ${instructor.lastName}`).join(', ')
);
case 'last': case 'last':
return instructors.map(instructor => instructor.lastName).join(', '); return prefix + instructors.map(instructor => instructor.lastName).join(', ');
default: default:
throw new Error(`Invalid Instructor String format: ${format}`); throw new Error(`Invalid Instructor String format: ${format}`);
} }
@@ -110,3 +123,15 @@ export type ScrapedRow = {
element: HTMLTableRowElement; element: HTMLTableRowElement;
course: Course | null; course: Course | null;
}; };
/**
* Options for how to format the instructor string
*/
type InstructorFormatOptions = {
/** a prefix to add to the string, ex: "with" Mike Scott */
prefix?: string;
/** The maximum number of instructors to show */
max?: number;
/** How do you want the names of the professors formatted */
format: 'abbr' | 'first_last' | 'last' | 'full_name';
};

View File

@@ -33,7 +33,13 @@ export default function CoursePopup({ course, onClose }: Props) {
#{course.uniqueId} #{course.uniqueId}
</Link> </Link>
</Text> </Text>
<Text size='medium'>
{course.getInstructorString({
prefix: 'with ',
format: 'first_last',
max: 3,
})}
</Text>
</Card> </Card>
</Popup> </Popup>
); );