refactor: extract shuffleArray to lib/utils, rename correctAnswers to terms

This commit is contained in:
lila 2026-04-28 13:17:24 +02:00
parent c46729f365
commit 2ff7d1759e
5 changed files with 59 additions and 60 deletions

View file

@ -115,42 +115,6 @@ export class InMemoryGameSessionStore implements GameSessionStore {
---
## 3. `shuffle` is defined after it's used
**Problem**
`shuffle` is called inside `createGameSession` but defined below it. It works at runtime (module evaluation order), but reads as if the file was written top-to-bottom without a plan.
```ts
// ❌ shuffle appears after the function that calls it
export const createGameSession = async (...) => {
const shuffledTexts = shuffle(optionTexts); // used here
};
const shuffle = <T>(array: T[]): T[] => { ... }; // defined down here
```
**Fix — move helpers to the top, exports to the bottom**
```ts
// ✅ utilities first, then exported functions
const shuffle = <T>(array: T[]): T[] => {
const result = [...array];
for (let i = result.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = result[i]!;
result[i] = result[j]!;
result[j] = temp;
}
return result;
};
export const createGameSession = async (...) => { ... };
export const evaluateAnswer = async (...) => { ... };
```
---
**Problem**
@ -181,8 +145,6 @@ The `z.coerce.number()` handles the case where the value arrives as a string fro
---
## 5. `correctAnswers` is a misleading variable name
**Problem**
The variable holds `terms` — word pairs fetched from the database. Calling them `correctAnswers` jumps ahead semantically; they only become "correct answers" once options are constructed around them.