bunch of refactor and styling changes, coming along nicely

This commit is contained in:
Sriram Hariharan
2023-03-07 01:42:26 -06:00
parent f48f39e67b
commit 04a82fb6a6
16 changed files with 225 additions and 63 deletions

View File

@@ -1,4 +1,5 @@
import { Serialized } from 'chrome-extension-toolkit';
import { capitalize } from '../util/string';
import { CourseSchedule } from './CourseSchedule';
/**
@@ -7,8 +8,8 @@ import { CourseSchedule } from './CourseSchedule';
*/
export type Instructor = {
fullName: string;
firstName?: string;
lastName?: string;
firstName: string;
lastName: string;
middleInitial?: string;
};
@@ -77,6 +78,8 @@ export class Course {
this.schedule = new CourseSchedule(course.schedule);
}
/**
* Get a string representation of the instructors for this course
* @param options - the options for how to format the instructor string
@@ -85,35 +88,25 @@ export class Course {
getInstructorString(options: InstructorFormatOptions): string {
const { max = 3, format, prefix = '' } = options;
if (!this.instructors.length) {
return `${prefix} Undecided`;
return `${prefix} TBA`;
}
const instructors = this.instructors.slice(0, max);
switch (format) {
case 'abbr':
return (
prefix +
instructors
.map(instructor => {
let firstInitial = instructor.firstName?.[0];
if (firstInitial) {
firstInitial += '. ';
}
return `${firstInitial}${instructor.lastName}`;
})
.join(', ')
);
case 'full_name':
return prefix + instructors.map(instructor => instructor.fullName).join(', ');
case 'first_last':
return (
prefix + instructors.map(instructor => `${instructor.firstName} ${instructor.lastName}`).join(', ')
);
case 'last':
return prefix + instructors.map(instructor => instructor.lastName).join(', ');
default:
throw new Error(`Invalid Instructor String format: ${format}`);
if (format === 'abbr') {
return prefix + instructors.map(i => `${capitalize(i.firstName[0])}. ${capitalize(i.lastName)}`).join(', ');
}
if (format === 'full_name') {
return prefix + instructors.map(i => capitalize(i.fullName)).join(', ');
}
if (format === 'first_last') {
return prefix + instructors.map(i => `${capitalize(i.firstName)} ${capitalize(i.lastName)}`).join(', ');
}
if (format === 'last') {
return prefix + instructors.map(i => i.lastName).join(', ');
}
throw new Error(`Invalid Instructor String format: ${format}`);
}
}

View File

@@ -3,11 +3,22 @@
* @input The string to capitalize.
*/
export function capitalize(input: string): string {
try {
return input.charAt(0).toUpperCase() + input.substring(1).toLowerCase();
} catch (err) {
return input;
let capitalized = '';
const words = input.split(' ');
for (const word of words) {
if (word.includes('-')) {
const hyphenatedWords = word.split('-');
for (const hyphenatedWord of hyphenatedWords) {
capitalized += `${capitalizeFirstLetter(hyphenatedWord)}-`;
}
capitalized = capitalized.substring(0, capitalized.length - 1);
} else {
capitalized += capitalizeFirstLetter(word);
}
capitalized += ' ';
}
return capitalized;
}
/**
@@ -16,7 +27,7 @@ export function capitalize(input: string): string {
* @returns the string with the first letter capitalized
*/
export function capitalizeFirstLetter(input: string): string {
return input.charAt(0).toUpperCase() + input.slice(1);
return input.charAt(0).toUpperCase() + input.slice(1).toLowerCase();
}
/**