ecclesia
Version:
Framework for political and electoral simulations
25 lines • 956 B
JavaScript
import { NumberCounter, DefaultMap } from "@gouvernathor/python/collections";
import { Scores } from "./tally";
export function tallySingleToSimple(ballots) {
return NumberCounter.fromKeys(ballots);
}
export function tallyApprovalToSimple(ballots) {
return NumberCounter.fromKeys(Array.from(ballots).flatMap(b => Array.from(b)));
}
export function tallyRankedToOrder(ballots) {
return Array.from(ballots);
}
/**
* This function assumes that the scores in each ballot are 0-based,
* going from 0, inclusive, to nScores, exclusive.
*/
export function tallyScoreToScores(ballots, { nScores }) {
const rawScores = new DefaultMap(() => Array(nScores).fill(0));
for (const ballot of ballots) {
for (const [candidate, scoreBasedOn0] of ballot.entries()) {
rawScores.get(candidate)[scoreBasedOn0] += 1;
}
}
return Scores.fromEntries(Array.from(rawScores.entries()));
}
//# sourceMappingURL=tallying.js.map