// 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] } export function removeFromArray(array, numToRemove) { return numToRemove ? array.slice(0, -numToRemove) : array } export function insertEntriesRandomlyIntoArray(array, count, entry = {}) { return Array.from({ length: count }).reduce((acc, _) => { // Generate a random index to insert the value const randomIndex = Math.floor(Math.random() * (array.length + 1)); // Insert the value into the array at the random index return [...acc.slice(0, randomIndex), entry, ...acc.slice(randomIndex)]; }, array) }