From 0ba9eeff43f3f782ca5e16360e04f6f2087341be Mon Sep 17 00:00:00 2001 From: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com> Date: Wed, 2 Oct 2024 06:55:38 -0700 Subject: [PATCH] refactor(api, curriculum): use the shared shuffleArray util (#56444) Co-authored-by: Oliver Eyton-Williams --- api-server/src/server/utils/exam.js | 16 +--------------- api/src/utils/exam.ts | 18 +----------------- curriculum/test/utils/sort-challenges.test.js | 11 ++--------- shared/utils/shuffle-array.ts | 5 ++++- 4 files changed, 8 insertions(+), 42 deletions(-) diff --git a/api-server/src/server/utils/exam.js b/api-server/src/server/utils/exam.js index 38262fc8f04..61c92323cd4 100644 --- a/api-server/src/server/utils/exam.js +++ b/api-server/src/server/utils/exam.js @@ -1,18 +1,4 @@ -function shuffleArray(arr) { - let currentIndex = arr.length, - randomIndex; - - while (currentIndex != 0) { - randomIndex = Math.floor(Math.random() * currentIndex); - currentIndex--; - [arr[currentIndex], arr[randomIndex]] = [ - arr[randomIndex], - arr[currentIndex] - ]; - } - - return arr; -} +import { shuffleArray } from '../../../../shared/utils/shuffle-array'; function filterDeprecated(arr) { return arr.filter(i => !i.deprecated); diff --git a/api/src/utils/exam.ts b/api/src/utils/exam.ts index e43fe12000c..94671fc8f85 100644 --- a/api/src/utils/exam.ts +++ b/api/src/utils/exam.ts @@ -1,23 +1,7 @@ import { Exam, Question } from '@prisma/client'; +import { shuffleArray } from './../../../shared/utils/shuffle-array'; import { UserExam, GeneratedExam } from './exam-types'; -function shuffleArray(arr: T[]): T[] { - let currentIndex: number = arr.length; - let randomIndex: number; - - while (currentIndex !== 0) { - randomIndex = Math.floor(Math.random() * currentIndex); - currentIndex--; - - [arr[currentIndex], arr[randomIndex]] = [ - arr[randomIndex] as T, - arr[currentIndex] as T - ]; - } - - return arr; -} - /** * Remove objects from array with deprecated: true. * diff --git a/curriculum/test/utils/sort-challenges.test.js b/curriculum/test/utils/sort-challenges.test.js index 0ad94607c60..a80c0b16ca4 100644 --- a/curriculum/test/utils/sort-challenges.test.js +++ b/curriculum/test/utils/sort-challenges.test.js @@ -1,3 +1,4 @@ +const { shuffleArray } = require('../../../shared/utils/shuffle-array'); const { sortChallenges } = require('./sort-challenges'); const challenges = [ @@ -222,7 +223,7 @@ const challenges = [ describe('sortChallenges', () => { it('sorts challenges by superblock, block and challenge order', () => { const copyOfChallenges = [...challenges]; - shuffle(copyOfChallenges); + shuffleArray(copyOfChallenges); const actualChallenges = sortChallenges(copyOfChallenges); expect(actualChallenges).toEqual(challenges); @@ -241,11 +242,3 @@ describe('sortChallenges', () => { expect(actualChallenges[0]).not.toEqual(copyOfChallenges[0]); }); }); - -// Use the Fisher–Yates shuffle algorithm to shuffle array -const shuffle = arr => { - for (let i = arr.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [arr[i], arr[j]] = [arr[j], arr[i]]; - } -}; diff --git a/shared/utils/shuffle-array.ts b/shared/utils/shuffle-array.ts index 0cc7dc5e1fb..66dfe450010 100644 --- a/shared/utils/shuffle-array.ts +++ b/shared/utils/shuffle-array.ts @@ -4,7 +4,10 @@ export const shuffleArray = (arrToShuffle: Array) => { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); - [arr[i], arr[j]] = [arr[j], arr[i]]; + + // We know that i and j are within the bounds of the array, TS doesn't + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + [arr[i], arr[j]] = [arr[j]!, arr[i]!]; } return arr;