UNPKG

ecclesia

Version:

Framework for political and electoral simulations

40 lines 1.33 kB
/** * The results of a binary vote. * The blank votes are not counted. To calculate a threshold on the whole * number of members, use vote.votesFor / house.nSeats. * To calculate the threshold on the number of duly elected members, use * vote.votesFor / sum(house.members.values()). */ export class Vote { constructor(votesFor, votesAgainst) { this.votesFor = votesFor; this.votesAgainst = votesAgainst; } /** * Returns the reverse of the vote, inverting the for/against ratio. * Simulates a vote on the opposite motion. */ get neg() { return new Vote(this.votesAgainst, this.votesFor); } get votesCast() { return this.votesFor + this.votesAgainst; } /** * Returns the ratio of votes for over the total number of votes cast. * If there are no votes cast, returns an Infinity. */ get ratio() { return this.votesFor / this.votesCast; } /** * Compares two votes in order of decreasing ratio. * The ties will be ordered by decreasing number of positive votes, * and then by the order they came in. * This is a function to be passed to Array.prototype.sort. */ static compare(a, b) { return (b.ratio - a.ratio) || (b.votesFor - a.votesFor); } } //# sourceMappingURL=vote.js.map