@proca/widget
Version:
Proca is an open-source campaign toolkit designed to empower activists and organisations in their digital advocacy efforts. It provides a flexible and customisable platform for creating and managing online petitions, email campaigns, and other forms of di
18 lines (15 loc) • 441 B
JavaScript
const shuffle = array => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
};
const sample = (array, sampleSize) => {
if (!sampleSize || sampleSize === 1) {
return [array[Math.floor(Math.random() * array.length)]];
}
const copy = [...array];
shuffle(copy);
return copy.slice(0, sampleSize);
};
export { shuffle, sample };