UNPKG

ecclesia

Version:

Framework for political and electoral simulations

89 lines (88 loc) 2.44 kB
import { Vote } from "../chunk-QR5MPQNM.js"; // src/actors/house.ts import { Counter } from "@gouvernathor/python/collections"; var District = class { constructor(electionMethod, voters, { identifier, nSeats } = {}) { this.electionMethod = electionMethod; this.voters = voters; this.identifier = identifier; this.nSeats = nSeats; } election(candidates) { return this.electionMethod(this.voters, candidates); } }; var House = class { constructor(districts, { name, majority = 0.5 } = {}) { if (!(districts instanceof Map)) { districts = new Map([...districts].map((d) => [d, new Counter()])); } this.districts = districts; this.name = name; this.majority = majority; } /** * Returns a Counter linking each party to the number of seats it holds, * regardless of the district. * For the array (with repetitions) of the individual members, use the * members.elements() method. */ get members() { const coun = new Counter(); for (const dmembers of this.districts.values()) { coun.update(dmembers); } return coun; } /** * If all districts support providing a theoretical number of seats, * returns the total. Otherwise, returns undefined. */ get nSeats() { let nSeats = 0; for (const district of this.districts.keys()) { if (district.nSeats === void 0) { return void 0; } nSeats += district.nSeats; } return nSeats; } /** * Triggers an election in each electoral district, returns the members result. */ election(candidates) { const members = new Counter(); for (const district of this.districts.keys()) { const elected = district.election(candidates); this.districts.set(district, elected); members.update(elected); } return members; } /** * Returns the state of the vote on something having an opinion. * The object of the vote may be a motion or bill, but also a person to elect * or to confirm. */ vote(target, { disagree }) { let votesFor = 0; let votesAgainst = 0; for (const [party, nSeats] of this.members) { const disag = disagree(party, target); if (disag > 0) { votesFor += nSeats; } else if (disag < 0) { votesAgainst += nSeats; } } return new Vote(votesFor, votesAgainst); } }; export { District, House }; //# sourceMappingURL=house.js.map