| 12345678910111213141516171819202122232425262728293031323334353637 |
- // Even-probability shuffle
- // https://en.m.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
- export function shuffle(array) {
- for (let i = array.length - 1; i > 0; i--) {
- let j = Math.floor(Math.random() * (i + 1));
- [array[i], array[j]] = [array[j], array[i]];
- }
- return array;
- }
- export function checkRemovedCount(oldArray, newArray, removedItems) {
- const expectedLength = oldArray.length - removedItems.length
- return newArray.length === expectedLength
- }
- export function splitArray(array, numPieces) {
- return array.reduce((splitArrays, item, index) => {
- const position = index % numPieces;
- return [
- ...splitArrays.slice(0, position),
- [...(splitArrays[position] || []), item],
- ...splitArrays.slice(position + 1)
- ];
- }, Array.from({ length: numPieces }, () => []));
- }
- export function annotateArray(labels, data) {
- return labels.reduce((zipped, label, index) => {
- return {...zipped, [label]: data[index]}
- }, {})
- }
- export function getRandomItem(array) {
- const index = Math.floor(Math.random() * array.length)
- return array[index]
- }
|