UNPKG

ecclesia

Version:

Framework for political and electoral simulations

42 lines 1.58 kB
import { max } from "@gouvernathor/python"; import { NumberCounter } from "@gouvernathor/python/collections"; import { AttributionFailure } from "../attribution"; /** * Creates an attribution method in which * the party with the most votes wins all the seats. * * Will throw an AttributionFailure error if no party wins any vote. */ export function plurality({ nSeats }) { const attrib = (votes, _rest = {}) => { const win = max(votes.keys(), p => votes.get(p)); if (votes.get(win) > 0) { return NumberCounter.fromEntries([[win, nSeats]]); } throw new AttributionFailure("No party won any vote"); }; attrib.nSeats = nSeats; return attrib; } /** * Creates an attribution method in which a candidate needs to reach * a certain percentage of the votes in order to win all the seats. * * If not party reaches the threshold, the contingency attribution method is called, * or if no contingency is provided, an AttributionFailure error is thrown. */ export function superMajority({ nSeats, threshold, contingency = null }) { const attrib = (votes, rest = {}) => { const win = max(votes.keys(), p => votes.get(p)); if ((votes.get(win) / votes.total) > threshold) { return NumberCounter.fromEntries([[win, nSeats]]); } if (contingency === null) { throw new AttributionFailure("No party reached the threshold"); } return contingency(votes, rest); }; attrib.nSeats = nSeats; return attrib; } //# sourceMappingURL=majorityFactory.js.map