Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/benchmarks/src/quizQuestions/bin/loadBadgeQuizQuestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,43 @@ import fs from "fs";
import path from "path";
import csv from "csv-parser";
import { QuizQuestionData, QuizQuestionDataSchema } from "../QuizQuestionData";
import { makeTags } from "./makeTags";

const testDataPath = path.resolve(__dirname, "..", "..", "..", "testData");
const csvFileInPath = path.resolve(testDataPath, "badge-questions.csv");
const jsonFileOutPath = path.resolve(testDataPath, "badge-questions.json");

const handleAnswers = (row: any) => {
const correctAnswers = row.Answer.trim()?.split("") || [];
Comment on lines +10 to +11
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of passing row: any, please make sure that the input is strongly typed. you can use zod to do any validation that you need.

const answers = ["A", "B", "C", "D", "E", "F"]
.map((label) => {
const isCorrect = correctAnswers.includes(label);
return {
answer: row[label],
isCorrect,
label,
};
})
.filter((answer) => answer.answer && answer.answer.trim() !== ""); // Remove empty answers
Comment on lines +12 to +21
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this code could be a little cleaner. rather than filtering at the end, can you slice the array of ["A", "B", "C", ...] to the correct length before running the map over it?

return answers;
};

const parseCSV = async (filePath: string): Promise<QuizQuestionData[]> => {
return new Promise((resolve, reject) => {
const results: QuizQuestionData[] = [];
fs.createReadStream(filePath)
.pipe(csv())
.on("data", (row) => {
try {
const answers = ["A", "B", "C", "D"].map((label, index) => ({
answer: row[label],
isCorrect: row.Answer === (index + 1).toString(),
label,
}));

const answers = handleAnswers(row);
const questionData: QuizQuestionData = QuizQuestionDataSchema.parse({
questionText: row["Question Text"],
title: row["Assessment"],
topicType: "badge", // Defaulting topic type
questionType: "singleCorrect", // Assuming single correct answer
questionType:
row["Answer"].length > 1 ? "multipleCorrect" : "singleCorrect",
Comment on lines +80 to +81
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider doing more typesafe parsing rather than checking string length (which isn't very resilient). for example, there could be an err in the spreadsheet

answers,
explanation: row["Reference"],
tags: row["tags"] ? row["tags"].split(",") : [],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tags: row["tags"] ? row["tags"].split(",") : [],
tags: row["tags"] ? row["tags"].split(",").map(t => t.trim()) : [],

again, a bit more resilient

});
questionData.tags = makeTags(questionData);
results.push(questionData);
} catch (error) {
console.error("Validation error:", error);
Expand Down