Skip to content

Commit 7d07b8a

Browse files
authored
fix(core): allow shuffle to return first element in first index (#3070)
1 parent 04e1f4f commit 7d07b8a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

libs/core/src/utils/shuffle.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,16 @@ describe('Core | Utils | shuffle', () => {
1111
});
1212
expect(initialArray).not.toEqual(result);
1313
});
14+
15+
it('should return array containing the same single element if input contains one element', () => {
16+
const initialArray = [1];
17+
const result = shuffle(initialArray);
18+
expect(initialArray).toEqual(result);
19+
});
20+
21+
it('should return an empty array if input is an empty array', () => {
22+
const initialArray = [];
23+
const result = shuffle(initialArray);
24+
expect(initialArray).toEqual(result);
25+
});
1426
});

libs/core/src/utils/shuffle.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
export const shuffle = <T>(array: T[]): T[] => {
66
const result = [];
77
array.forEach((el, i) => {
8-
const s = Math.floor(Math.random() * i);
8+
const s = Math.floor(Math.random() * (i + 1));
99
if( s !== i ){
1010
result[i] = result[s];
1111
}

0 commit comments

Comments
 (0)