Small refactor (hopefully appease CodeFactor?)

This commit is contained in:
Jaxon Lewis-Wilson
2026-01-29 14:50:48 +08:00
parent 9ad90e9416
commit cc3f06b383
2 changed files with 80 additions and 65 deletions
@@ -8,7 +8,7 @@ import { type Plugin } from "@/plugins/core/types";
import stringToHTML from "@/seqta/utils/stringToHTML"; import stringToHTML from "@/seqta/utils/stringToHTML";
import { waitForElm } from "@/seqta/utils/waitForElm"; import { waitForElm } from "@/seqta/utils/waitForElm";
import ReactFiber from "@/seqta/utils/ReactFiber.ts"; import ReactFiber from "@/seqta/utils/ReactFiber.ts";
import { clearStuck, createWeightLabel, getClassByPattern, initStorage, letterToNumber, parseAssessments, parseGrade} from "./utils.ts"; import { clearStuck, getClassByPattern, initStorage, letterToNumber, parseAssessments, processAssessments} from "./utils.ts";
// Storage // Storage
interface weightingsStorage { interface weightingsStorage {
@@ -128,68 +128,13 @@ const assessmentsAveragePlugin: Plugin<typeof settings, weightingsStorage> = {
!item.querySelector(`[class*='AssessmentItem__title___']`)?.textContent?.includes("Subject Average"), !item.querySelector(`[class*='AssessmentItem__title___']`)?.textContent?.includes("Subject Average"),
); );
// Match marks to assessment items and calculate weighted average // Tally up weightedTotal, totalWeight, count, determine if weighting is accurate, and display a weight label per assessment
let weightedTotal = 0; const {
let totalWeight = 0; weightedTotal,
let hasInaccurateWeighting = false; totalWeight,
let count = 0; hasInaccurateWeighting,
count,
// Iterate through assessments for processing } = await processAssessments(api, assessmentItems);
for (const assessmentItem of assessmentItems) {
const gradeElement = assessmentItem.querySelector(
`[class*='Thermoscore__text___']`,
);
if (!gradeElement) continue;
const grade = parseGrade(gradeElement.textContent || "");
if (grade <= 0) continue;
const titleEl = assessmentItem.querySelector(
`[class*='AssessmentItem__title___']`,
);
if (!titleEl) continue;
const title = titleEl.textContent?.trim();
if (!title) continue;
// Get correlated assessment ID in order to fetch weightings
const assessmentID = api.storage.assessments?.[title];
const weighting = assessmentID
? api.storage.weightings?.[assessmentID]
: undefined;
// Creates a weighting label next to the average score
createWeightLabel(assessmentItem, weighting)
// Check if weighting is unavailable or still processing
if (
weighting === null ||
weighting === undefined ||
weighting === "N/A" ||
weighting === "processing"
) {
hasInaccurateWeighting = true;
// Fall back to equal weighting if unavailable
weightedTotal += grade;
totalWeight += 1;
} else {
const weight = parseFloat(weighting);
// If weight is a positive number, add to total
if (!isNaN(weight) && weight >= 0) {
weightedTotal += grade * weight;
totalWeight += weight;
} else {
// Invalid weight, use equal weighting
weightedTotal += grade;
totalWeight += 1;
hasInaccurateWeighting = true;
}
}
count++;
}
if (!count || totalWeight === 0) return; if (!count || totalWeight === 0) return;
@@ -60,7 +60,7 @@ export const letterToNumber: Record<string, number> = {
F: 0, F: 0,
}; };
export function parseGrade(text: string): number { function parseGrade(text: string): number {
const str = text.trim().toUpperCase(); const str = text.trim().toUpperCase();
if (str.includes("/")) { if (str.includes("/")) {
const [raw, max] = str.split("/").map((n) => parseFloat(n)); const [raw, max] = str.split("/").map((n) => parseFloat(n));
@@ -72,7 +72,7 @@ export function parseGrade(text: string): number {
return letterToNumber[str] ?? 0; return letterToNumber[str] ?? 0;
} }
export function createWeightLabel(assessmentItem: Element, weighting: string | undefined) { function createWeightLabel(assessmentItem: Element, weighting: string | undefined) {
const statsContainer = assessmentItem.querySelector( const statsContainer = assessmentItem.querySelector(
`[class*='AssessmentItem__stats___']`, `[class*='AssessmentItem__stats___']`,
) as HTMLElement; ) as HTMLElement;
@@ -553,3 +553,73 @@ export async function parseAssessments(api: any) {
// Dispatch for all assessments asynchronously // Dispatch for all assessments asynchronously
await Promise.all(marks.map((mark: any) => handleWeightings(mark, api))); await Promise.all(marks.map((mark: any) => handleWeightings(mark, api)));
} }
// Tally up weightedTotal, totalWeight, count, determine if weighting is accurate, and display a weight label per assessment
export async function processAssessments(api: any, assessmentItems: Element[]) {
let weightedTotal = 0;
let totalWeight = 0;
let hasInaccurateWeighting = false;
let count = 0;
for (const assessmentItem of assessmentItems) {
const gradeElement = assessmentItem.querySelector(
`[class*='Thermoscore__text___']`,
);
if (!gradeElement) continue;
const grade = parseGrade(gradeElement.textContent || "");
if (grade <= 0) continue;
const titleEl = assessmentItem.querySelector(
`[class*='AssessmentItem__title___']`,
);
if (!titleEl) continue;
const title = titleEl.textContent?.trim();
if (!title) continue;
// Get correlated assessment ID in order to fetch weightings
const assessmentID = api.storage.assessments?.[title];
const weighting = assessmentID
? api.storage.weightings?.[assessmentID]
: undefined;
// Creates a weighting label next to the average score
createWeightLabel(assessmentItem, weighting);
// Check if weighting is unavailable or still processing
if (
weighting === null ||
weighting === undefined ||
weighting === "N/A" ||
weighting === "processing"
) {
hasInaccurateWeighting = true;
// Fall back to equal weighting if unavailable
weightedTotal += grade;
totalWeight += 1;
} else {
const weight = parseFloat(weighting);
// If weight is a positive number, add to total
if (!isNaN(weight) && weight >= 0) {
weightedTotal += grade * weight;
totalWeight += weight;
} else {
// Invalid weight, use equal weighting
weightedTotal += grade;
totalWeight += 1;
hasInaccurateWeighting = true;
}
}
count++;
}
return {
weightedTotal,
totalWeight,
hasInaccurateWeighting,
count,
};
}