Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shuffle cards to review #18

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 42 additions & 0 deletions src/lib/array/shuffle-array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {describe, it, expect} from 'vitest'
import {shuffleArray} from './shuffle-array.ts'

describe('shuffleArray function', () => {
it('should shuffle an array of numbers', () => {
const array = [1, 2, 3, 4, 5];
const shuffled = shuffleArray([...array]);
expect(shuffled).not.toEqual(array);
});

it('should shuffle by reference', () => {
const array = [1, 2, 3, 4, 5];
const initialArray = [...array]
shuffleArray(array);
expect(array).not.toEqual(initialArray);
});

it('should shuffle an array of strings', () => {
const array = ['a', 'b', 'c', 'd', 'e'];
const shuffled = shuffleArray([...array]);
expect(shuffled).not.toEqual(array);
});

it('should handle an empty array', () => {
const array: unknown[] = [];
const shuffled = shuffleArray(array);
expect(shuffled).toEqual([]);
});

it('should retain all elements', () => {
const array = [1, 2, 3, 4, 5];
const shuffled = shuffleArray([...array]);
expect(shuffled.sort()).toEqual(array.sort());
});

it('should provide a random order', () => {
const array = [1, 2, 3, 4, 5];
const firstShuffle = shuffleArray([...array]);
const secondShuffle = shuffleArray([...array]);
expect(firstShuffle).not.toEqual(secondShuffle);
});
});
15 changes: 15 additions & 0 deletions src/lib/array/shuffle-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const shuffleArray = <T>(array: T[]): T[] => {
let currentIndex = array.length,
randomIndex;

while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex],
];
}

return array;
};
2 changes: 2 additions & 0 deletions src/store/card-under-review-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class CardUnderReviewStore {
back: string;
example: string | null = null;
deckName?: string;
deckId?: number;
deckSpeakLocale: string | null = null;
deckSpeakField: string | null = null;

Expand All @@ -33,6 +34,7 @@ export class CardUnderReviewStore {
this.back = card.back;
this.example = card.example;
this.deckName = deck.name;
this.deckId = deck.id;
this.deckSpeakLocale = deck.speak_locale;
this.deckSpeakField = deck.speak_field;

Expand Down
8 changes: 8 additions & 0 deletions src/store/review-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ vi.mock("./deck-list-store.ts", () => {
};
});

vi.mock('../lib/array/shuffle-array.ts', () => {
return {
shuffleArray: (array: unknown[]) => {
return array;
},
};
})

const cardToSnapshot = (card: CardUnderReviewStore) => ({
back: card.back,
deckName: card.deckName,
Expand Down
19 changes: 12 additions & 7 deletions src/store/review-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
deckListStore,
DeckWithCardsWithReviewType,
} from "./deck-list-store.ts";
import { shuffleArray } from "../lib/array/shuffle-array.ts";

type ReviewResult = {
forgotIds: number[];
Expand Down Expand Up @@ -37,10 +38,12 @@ export class ReviewStore {
if (!deck.cardsToReview.length) {
return;
}
deck.cardsToReview.forEach((card) => {
this.cardsToReview.push(new CardUnderReviewStore(card, deck));
});
const cardsToReview = deck.cardsToReview.map(
(card) => new CardUnderReviewStore(card, deck),
);
shuffleArray(cardsToReview);

this.cardsToReview = cardsToReview;
this.initialCardCount = this.cardsToReview.length;
this.currentCardId = this.cardsToReview[0].id;
if (this.cardsToReview.length > 1) {
Expand All @@ -59,11 +62,13 @@ export class ReviewStore {
}

myDecks.forEach((deck) => {
deck.cardsToReview
const cardsToRepeat = deck.cardsToReview
.filter((card) => card.type === "repeat")
.forEach((card) => {
this.cardsToReview.push(new CardUnderReviewStore(card, deck));
});
.map((card) => new CardUnderReviewStore(card, deck));

shuffleArray(cardsToRepeat);

this.cardsToReview.push(...cardsToRepeat);
});

if (!this.cardsToReview.length) {
Expand Down