Skip to content

Commit

Permalink
fix(core): allow shuffle to return first element in first index (#3070)
Browse files Browse the repository at this point in the history
  • Loading branch information
gracetxgao authored Sep 17, 2024
1 parent 04e1f4f commit 7d07b8a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions libs/core/src/utils/shuffle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ describe('Core | Utils | shuffle', () => {
});
expect(initialArray).not.toEqual(result);
});

it('should return array containing the same single element if input contains one element', () => {
const initialArray = [1];
const result = shuffle(initialArray);
expect(initialArray).toEqual(result);
});

it('should return an empty array if input is an empty array', () => {
const initialArray = [];
const result = shuffle(initialArray);
expect(initialArray).toEqual(result);
});
});
2 changes: 1 addition & 1 deletion libs/core/src/utils/shuffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
export const shuffle = <T>(array: T[]): T[] => {
const result = [];
array.forEach((el, i) => {
const s = Math.floor(Math.random() * i);
const s = Math.floor(Math.random() * (i + 1));
if( s !== i ){
result[i] = result[s];
}
Expand Down

0 comments on commit 7d07b8a

Please sign in to comment.