Skip to content

Commit c67d0d2

Browse files
authored
Merge pull request #102 from PrestaShopCorp/fixShuffle
Fix for shuffle method and remove unused code
2 parents 40736d9 + 5bc2aaa commit c67d0d2

File tree

3 files changed

+80
-255
lines changed

3 files changed

+80
-255
lines changed

src/common/utils/shuffle.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ function rndNext(min, max) {
55
}
66

77
function shuffle (arr, from, to) {
8-
if (!to) {
9-
to = arr.length - 1;
8+
if (arr.length <= 1){
9+
return arr;
1010
}
1111
if (!from) {
1212
from = 0;
1313
}
14+
if (!to) {
15+
to = arr.length - 1;
16+
}
1417
const newArr = [...arr];
1518
if (from >= to) return;
1619
for (var current = from; current <= to; ++current) {

src/core/clusterDnsEndPointDiscoverer.js

Lines changed: 0 additions & 253 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const shuffle = require('../../../../src/common/utils/shuffle');
2+
3+
describe('shuffle', () => {
4+
test('Should return a shuffle array (same size, different order)', async () => {
5+
// arrange
6+
const tArray = [1,2,3,4,5,6,7,8,9];
7+
// act
8+
const result = shuffle(tArray);
9+
// assert
10+
expect(result).toHaveLength(tArray.length);
11+
expect(result).not.toEqual(tArray);
12+
});
13+
14+
test('Should return a shuffle array for array of size 1', async () => {
15+
// arrange
16+
const tArray = [1];
17+
// act
18+
const result = shuffle(tArray);
19+
// assert
20+
expect(result).toHaveLength(tArray.length);
21+
expect(result).toEqual(tArray);
22+
});
23+
24+
test('Should return a shuffle array for array of size 0', async () => {
25+
// arrange
26+
const tArray = [];
27+
// act
28+
const result = shuffle(tArray);
29+
// assert
30+
expect(result).toHaveLength(tArray.length);
31+
expect(result).toEqual(tArray);
32+
});
33+
34+
test('Should return a shuffle only from a starting point', async () => {
35+
// arrange
36+
const tArray = [1,2,3,4,5,6,7,8,9];
37+
// act
38+
const result = shuffle(tArray, 2);
39+
// assert
40+
expect(result).toHaveLength(tArray.length);
41+
expect(result).not.toEqual(tArray);
42+
const fixedOrigin = [...tArray].splice(0, 2);
43+
const shuffledOrigin = [...tArray].splice(2);
44+
const fixedResult= [...result].splice(0, 2);
45+
const shuffledResult = [...result].splice(2);
46+
expect(fixedResult).toHaveLength(fixedOrigin.length);
47+
expect(fixedResult).toEqual(fixedOrigin);
48+
expect(shuffledResult).toHaveLength(shuffledOrigin.length);
49+
expect(shuffledResult).not.toEqual(shuffledOrigin);
50+
});
51+
52+
test('Should return a shuffle only from a starting point to and end point', async () => {
53+
// arrange
54+
const tArray = [1,2,3,4,5,6,7,8,9];
55+
// act
56+
const result = shuffle(tArray, 2, 4);
57+
// assert
58+
expect(result).toHaveLength(tArray.length);
59+
expect(result).not.toEqual(tArray);
60+
const fixedStartOrigin = [...tArray].splice(0, 2);
61+
const fixedEndOrigin = [...tArray].splice(4);
62+
const shuffledOrigin = [...tArray].splice(2, 4);
63+
const fixedStartResult= [...result].splice(0, 2);
64+
const fixedEndResult = [...tArray].splice(4);
65+
const shuffledResult = [...result].splice(2, 4);
66+
expect(fixedStartResult).toHaveLength(fixedStartOrigin.length);
67+
expect(fixedStartResult).toEqual(fixedStartOrigin);
68+
69+
expect(fixedEndResult).toHaveLength(fixedEndOrigin.length);
70+
expect(fixedEndResult).toEqual(fixedEndOrigin);
71+
72+
expect(shuffledResult).toHaveLength(shuffledOrigin.length);
73+
expect(shuffledResult).not.toEqual(shuffledOrigin);
74+
});
75+
});

0 commit comments

Comments
 (0)