Skip to content

Commit 17b4c98

Browse files
committed
After review
1 parent ce03607 commit 17b4c98

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

lib/codecept.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,20 +194,15 @@ class Codecept {
194194
}
195195

196196
/**
197-
* Fisher-Yates algorithm for shuffle
197+
* FisherYates shuffle (non-mutating)
198198
*/
199-
shuffle(arrayToShuffle) {
200-
var i = arrayToShuffle.length,
201-
j,
202-
temp
203-
while (--i > 0) {
204-
j = Math.floor(Math.random() * (i + 1))
205-
temp = arrayToShuffle[j]
206-
arrayToShuffle[j] = arrayToShuffle[i]
207-
arrayToShuffle[i] = temp
199+
shuffle(array) {
200+
const arr = [...array] // clone to avoid mutating input
201+
for (let i = arr.length - 1; i > 0; i--) {
202+
const j = Math.floor(Math.random() * (i + 1))
203+
;[arr[i], arr[j]] = [arr[j], arr[i]] // swap
208204
}
209-
210-
return arrayToShuffle
205+
return arr
211206
}
212207

213208
/**

0 commit comments

Comments
 (0)