40 lines
891 B
JavaScript
40 lines
891 B
JavaScript
function createPoll(title) {
|
|
return {
|
|
title,
|
|
options: []
|
|
};
|
|
}
|
|
function addOption(poll, text) {
|
|
poll.options.push({
|
|
id: crypto.randomUUID(),
|
|
text,
|
|
votes: []
|
|
});
|
|
}
|
|
function hasVoted(poll, optionId, peerId) {
|
|
const option = poll.options.find((o) => o.id === optionId);
|
|
if (!option) return false;
|
|
return option.votes.includes(peerId);
|
|
}
|
|
function vote(poll, optionId, peerId) {
|
|
const option = poll.options.find((o) => o.id === optionId);
|
|
if (!option) return;
|
|
if (option.votes.includes(peerId)) return;
|
|
option.votes.push(peerId);
|
|
}
|
|
function unvote(poll, optionId, peerId) {
|
|
const option = poll.options.find((o) => o.id === optionId);
|
|
if (!option) return;
|
|
const idx = option.votes.indexOf(peerId);
|
|
if (idx !== -1) {
|
|
option.votes.splice(idx, 1);
|
|
}
|
|
}
|
|
export {
|
|
addOption as a,
|
|
createPoll as c,
|
|
hasVoted as h,
|
|
unvote as u,
|
|
vote as v
|
|
};
|