Last active
June 22, 2020 08:20
-
-
Save reimertz/9ed5c1fd257389f149042e86cf15576f to your computer and use it in GitHub Desktop.
Function to create groups based on Google Meet participanst.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
optimalSize; Group size, if uneven, last group will contain one more member. | |
ignoreNames; A list if names you'd like to ignore. Good when some people can't participate. | |
*/ | |
groupPeople1337 = (optimalSize = 2, ignoreNames = []) => { | |
const onlyUnique = (value, index, self) => { | |
return self.indexOf(value) === index; | |
} | |
const shuffleArray = (arr) => { | |
for (let i = arr.length - 1; i > 0; i--) { | |
const j = Math.floor(Math.random() * (i + 1)); | |
[arr[i], arr[j]] = [arr[j], arr[i]]; | |
} | |
return arr | |
} | |
const chunk = array => | |
Array.from({ | |
length: Math.ceil(array.length / optimalSize) | |
}, (v, i) => | |
array.slice(i * optimalSize, i * optimalSize + optimalSize) | |
); | |
const getNames = () => { | |
const elements = Array.from(document.querySelectorAll('[data-participant-id]')) | |
const names = elements.map(p => p.getAttribute('data-sort-key').split('spaces/')[0]) | |
return shuffleArray(names) | |
} | |
const people = getNames() | |
const filteredPeople = people | |
.filter(onlyUnique) | |
.filter(person => !ignoreNames.some(name => person.toLowerCase().includes(name.toLowerCase()))) | |
if (people.length === 0) { | |
return alert('You need to have the Members tab open for this to work') | |
} | |
if (people.length === 1) { | |
return alert('There is only you here') | |
} | |
if (filteredPeople.length <= 1) { | |
return alert('After filtering people out, there is no one left.') | |
} | |
let groups = chunk(filteredPeople, optimalSize) | |
if (groups[groups.length-1].length === 1) { | |
const singleGroup = groups.pop() | |
groups[groups.length-1].push(singleGroup[0]) | |
} | |
return groups | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment