Skip to content

Commit 70747cf

Browse files
committed
Added array shuffle extension method
1 parent 976cc4e commit 70747cf

File tree

4 files changed

+78
-36
lines changed

4 files changed

+78
-36
lines changed

MADE.Collections/package-lock.json

Lines changed: 30 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MADE.Collections/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
},
3838
"homepage": "https://github.com/MADE-Apps/MADE.js#readme",
3939
"devDependencies": {
40-
"@types/jest": "^27.4.0",
41-
"jest": "^27.4.7",
42-
"ts-jest": "^27.1.2",
43-
"typescript": "^4.5.4"
40+
"@types/jest": "^27.4.1",
41+
"jest": "^27.5.1",
42+
"ts-jest": "^27.1.4",
43+
"typescript": "^4.6.3"
4444
}
4545
}

MADE.Collections/src/array/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function potentialIndexOf<T>(array: T[], value: T, predicate: (value: T,
124124
}
125125

126126
/**
127-
*Inserts an item to the specified array at the potential index determined by the predicate.
127+
* Inserts an item to the specified array at the potential index determined by the predicate.
128128
* @param {T[]} array - The array of items the item should be inserted into.
129129
* @param {T} value - The object to insert into the array.
130130
* @param {(value: T, item: T) => boolean} predicate - The function to determine the position of the item based on the provided value.
@@ -137,3 +137,17 @@ export function insertAtPotentialIndex<T>(array: T[], value: T, predicate: (valu
137137
}
138138
return potentialIndex;
139139
}
140+
141+
/**
142+
* Creates a new array from the specified and shuffles the elements randomly.
143+
* @param {T[]} array - The array of items to shuffle.
144+
* @return {T[]} The new shuffled array of items.
145+
*/
146+
export function shuffle<T>(array: T[]): T[] {
147+
const result = array.slice();
148+
for (let i = result.length - 1; i > 0; i--) {
149+
const j = Math.floor(Math.random() * (i + 1));
150+
[result[i], result[j]] = [result[j], result[i]];
151+
}
152+
return result;
153+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { shuffle } from "../../src";
2+
3+
describe("when shuffling items", () => {
4+
it("should shuffle item order randomly", () => {
5+
// Arrange
6+
const items = [1, 2, 3, 4, 5];
7+
8+
// Act
9+
var shuffled = shuffle(items);
10+
11+
// Assert
12+
expect(shuffled).not.toEqual([1, 2, 3, 4, 5]);
13+
});
14+
15+
it("should contain same items after shuffle", () => {
16+
// Arrange
17+
const items = [1, 2, 3, 4, 5];
18+
19+
// Act
20+
var shuffled = shuffle(items);
21+
22+
// Assert
23+
expect(shuffled).toContain(1);
24+
expect(shuffled).toContain(2);
25+
expect(shuffled).toContain(3);
26+
expect(shuffled).toContain(4);
27+
expect(shuffled).toContain(5);
28+
});
29+
});

0 commit comments

Comments
 (0)