Skip to content

Commit d91a028

Browse files
committed
fix(utils): randomizeArrayWithFixedElements et shouldBeRandomized suivent maintenant la nouvelle syntaxe (avec un objet plutôt qu'un tableau)
1 parent 30749b8 commit d91a028

4 files changed

Lines changed: 31 additions & 31 deletions

File tree

app/js/utils/arrays.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function randomizeArrayWithFixedElements(array) {
4242

4343
// On distingue les éléments fixes et les éléments à ordonner de manière aléatoire
4444
array.forEach(function (element) {
45-
if (!element[2]) {
45+
if (!element.isRandom) {
4646
fixedElements.push(element);
4747
} else {
4848
randomizableElements.push(element);
@@ -55,7 +55,7 @@ export function randomizeArrayWithFixedElements(array) {
5555
// On reconstruit le tableau en réinsérant les éléments fixes au bon endroit
5656
var finalArray = [];
5757
array.forEach(function (element) {
58-
if (!element[2]) {
58+
if (!element.isRandom) {
5959
finalArray.push(element);
6060
} else {
6161
finalArray.push(randomizableElements.shift());
@@ -70,7 +70,7 @@ export function shouldBeRandomized(array) {
7070
if (Array.isArray(array)) {
7171
const arrayLength = array.length;
7272
for (let i = 0; i < arrayLength; i++) {
73-
if (array[i][2] === true) {
73+
if (array[i].isRandom === true) {
7474
return true;
7575
}
7676
}

app/script.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/script.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/unit/utils/arrays.spec.mjs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ describe("shuffleArray", () => {
122122
describe("randomizeArrayWithFixedElements", () => {
123123
it("returns an array of the same length as the input", () => {
124124
const input = [
125-
["A", 0, false],
126-
["B", 1, true],
127-
["C", 2, false],
128-
["D", 3, true],
125+
{ text: "A", link: "url0", isRandom: false },
126+
{ text: "B", link: "url1", isRandom: true },
127+
{ text: "C", link: "url2", isRandom: false },
128+
{ text: "D", link: "url3", isRandom: true },
129129
];
130130

131131
const result = randomizeArrayWithFixedElements([...input]);
@@ -134,10 +134,10 @@ describe("randomizeArrayWithFixedElements", () => {
134134

135135
it("keeps fixed elements in their original positions", () => {
136136
const input = [
137-
["A", 0, false],
138-
["B", 1, true],
139-
["C", 2, false],
140-
["D", 3, true],
137+
{ text: "A", link: "url0", isRandom: false },
138+
{ text: "B", link: "url1", isRandom: true },
139+
{ text: "C", link: "url2", isRandom: false },
140+
{ text: "D", link: "url3", isRandom: true },
141141
];
142142

143143
const result = randomizeArrayWithFixedElements([...input]);
@@ -147,10 +147,10 @@ describe("randomizeArrayWithFixedElements", () => {
147147

148148
it("preserves the set of randomizable elements but in different order", () => {
149149
const input = [
150-
["X", 0, false],
151-
["Y", 1, true],
152-
["Z", 2, true],
153-
["W", 3, false],
150+
{ text: "X", link: "url0", isRandom: false },
151+
{ text: "Y", link: "url1", isRandom: true },
152+
{ text: "Z", link: "url2", isRandom: true },
153+
{ text: "W", link: "url3", isRandom: false },
154154
];
155155

156156
const originalRandomizables = input.filter((el) => el[2]);
@@ -162,9 +162,9 @@ describe("randomizeArrayWithFixedElements", () => {
162162

163163
it("handles an array with only fixed elements", () => {
164164
const input = [
165-
["L", 0, false],
166-
["M", 1, false],
167-
["N", 2, false],
165+
{ text: "L", link: "url0", isRandom: false },
166+
{ text: "M", link: "url1", isRandom: false },
167+
{ text: "N", link: "url2", isRandom: false },
168168
];
169169

170170
const result = randomizeArrayWithFixedElements([...input]);
@@ -173,13 +173,13 @@ describe("randomizeArrayWithFixedElements", () => {
173173

174174
it("produces different random arrangements across multiple calls", () => {
175175
const input = [
176-
["A", 0, false],
177-
["B", 1, true],
178-
["C", 2, true],
179-
["D", 3, true],
180-
["E", 4, false],
181-
["F", 4, true],
182-
["G", 4, true],
176+
{ text: "A", link: "url0", isRandom: false },
177+
{ text: "B", link: "url1", isRandom: true },
178+
{ text: "C", link: "url2", isRandom: true },
179+
{ text: "D", link: "url3", isRandom: true },
180+
{ text: "E", link: "url4", isRandom: false },
181+
{ text: "F", link: "url4", isRandom: true },
182+
{ text: "G", link: "url4", isRandom: true },
183183
];
184184

185185
const arrangements = new Set();
@@ -195,7 +195,7 @@ describe("randomizeArrayWithFixedElements", () => {
195195
result[5],
196196
result[6],
197197
]
198-
.map((el) => el[0]) // use element name for comparison
198+
.map((el) => el.text) // use element name for comparison
199199
.join(",");
200200

201201
arrangements.add(randomizedPart);
@@ -209,9 +209,9 @@ describe("randomizeArrayWithFixedElements", () => {
209209
describe("shouldBeRandomized", () => {
210210
it("returns true if at least one element has true as the third value", () => {
211211
const input = [
212-
["Option A", 0, false],
213-
["Option B", 1, true],
214-
["Option C", 2, false],
212+
{ text: "Option A", link: "url0", isRandom: false },
213+
{ text: "Option B", link: "url1", isRandom: true },
214+
{ text: "Option C", link: "url2", isRandom: false },
215215
];
216216
const result = shouldBeRandomized(input);
217217
expect(result).toBeTrue();

0 commit comments

Comments
 (0)