Skip to content

Commit f5582e5

Browse files
authored
Merge pull request #932 from revisit-studies/zc/sequencesBugFix
Fixing problem introduced by new shuffling
2 parents ec86490 + 1e5d107 commit f5582e5

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/utils/handleRandomSequences.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// eslint-disable-next-line import/no-unresolved
22
import latinSquare from '@quentinroy/latin-square';
3+
import isEqual from 'lodash.isequal';
34
import { ComponentBlock, DynamicBlock, StudyConfig } from '../parser/types';
45
import { Sequence } from '../store/types';
56
import { isDynamicBlock } from '../parser/utils';
@@ -57,7 +58,7 @@ function _componentBlockToSequence(
5758
for (let i = 0; i < computedComponents.length; i += 1) {
5859
const curr = computedComponents[i];
5960
if (typeof curr !== 'string' && !Array.isArray(curr)) {
60-
const index = order.components.indexOf(curr);
61+
const index = order.components.findIndex((c) => isEqual(c, curr));
6162
computedComponents[i] = _componentBlockToSequence(curr, latinSquareObject, `${path}-${index}`) as unknown as ComponentBlock;
6263
}
6364
}
@@ -122,7 +123,7 @@ function componentBlockToSequence(
122123
return _componentBlockToSequence(orderCopy, latinSquareObject, 'root');
123124
}
124125

125-
function _createRandomOrders(order: StudyConfig['sequence'], paths: string[], path: string, index = 0) {
126+
function _createRandomOrders(order: StudyConfig['sequence'], paths: string[], path: string, index: number) {
126127
const newPath = path.length > 0 ? `${path}-${index}` : 'root';
127128
if (order.order === 'latinSquare') {
128129
paths.push(newPath);
@@ -162,7 +163,8 @@ function generateLatinSquare(config: StudyConfig, path: string) {
162163
});
163164

164165
const options = (locationInSequence as ComponentBlock).components.map((c: unknown, i: number) => (typeof c === 'string' ? c : `_componentBlock${i}`));
165-
const newSquare: string[][] = latinSquare<string>(options.sort(() => 0.5 - Math.random()), true);
166+
shuffle(options);
167+
const newSquare: string[][] = latinSquare<string>(options, true);
166168
return newSquare;
167169
}
168170

0 commit comments

Comments
 (0)