/** * This module provides classes and functions for tabulating STV (Single Transferable Vote) elections. * * ## Installation * ``` * npm install single-transferable-vote * ``` * ## Usage * ```ts * import { Election, Ballot } from 'single-transferable-vote' * ``` * @packageDocumentation */ /** Fractional is deterministic, while random or other sampling methods could unfairly weight certain votes. */ export type TransferType = "random" | "fractional"; export type ThresholdType = "Droop" | "Hare" | "ERS97"; /** The name of a candidate */ export type Candidate = string; /** A single voter's ballot, in order of preference */ export declare class Ballot { votes: Candidate[]; weight: number; constructor(votes: Candidate[], weight?: number); toJSON(): { votes: string[]; weight: number; }; } /** Flattens an array of simple objects (eg. `string`s) and makes them unique */ export declare const getFlatUniqueArray: (array: T[][]) => T[]; /** * Returns the threshold for a given number of votes and seats. * ERS97 is identical to Droop during this calculation—it only affects * the number of votes transferred in dynamic threshold systems. */ export declare const calculateThreshold: (ballotCount: number, seats: number, type?: ThresholdType, fractional?: boolean) => number; /** * Gets next preferences for a candidate from a list of votes. * @example * input: * ```ts * getVoterPreferencesForCandidate( * [ * ["A", "B", "C"], * ["A", "B", "C"], * ["A", "C"] * ], * "A") * ``` * output: * ```ts * { * B: 2/3, * C: 1/3 * } * ``` */ export declare const getVoterPreferencesForCandidate: (ballots: Ballot[], candidate: Candidate, roundIndex?: number) => { [preference: string]: number; }; /** A single round's result, including active candidates, total votes, and the winner */ export declare class Round { /** Input ballots */ ballots: Ballot[]; parent: Election; /** Elected candidates from the previous round */ electedAtStart: Set; /** Eliminated candidates from the previous round */ eliminatedAtStart: Set; /** Log of events in this round */ events: string[]; /** Links between candidates in this round. Useful for sankey visualization. */ links: { source: string; target: string; value: number; }[]; /** Index of round in parent `Election`. Needed for `links` */ index: number; constructor(parent?: Election); /** Calculate votes by adding the current this.ballots[i].votes[0] * this.ballots[i].weight */ get votes(): { [candidate: string]: number; }; /** Number of votes in this round */ get totalVotes(): number; /** Same as `totalVotes`, but ignores "No preference" */ get totalActiveVotes(): number; get currentlyAvailableSeats(): number; /** Threshold needed to be elected in this round */ get threshold(): number; /** Deduped list of candidates in the ballots */ get candidates(): string[]; /** Candidates sorted from most votes to fewest */ get sortedCandidates(): string[]; get hopefulCandidates(): string[]; /** Candidates that surpassed the threshold in this round */ get newWinners(): string[]; get losers(): string[]; /** Transfers votes from a winning candidate to their voters' next preference */ transferSurplus(candidate: Candidate, percentage: number): Ballot[]; /** Removes a candidate from subsequent rounds and redistributes all of their votes */ eliminateCandidate(candidate: Candidate): Ballot[]; /** for ballots that have the candidate in 1st, splice out the first item and redistribute the votes with weighting * @todo prevent votes from being redistributed to the an already-winning candidate */ redistributeVotes(candidate: Candidate, percentage: number, keep?: boolean): Ballot[]; toJSON(): { votes: { [candidate: string]: number; }; total: number; }; } /** * Run instant runoff tabulations for each round, adding calculated rounds to `rounds`. * Use single transferable vote (STV) to elect `winnersCount` candidates. * 1. Count the first choices for each candidate * 2. While there are still seats available, run the following: * 1. sort candidates by votes descending * 2. if a candidate has more votes than the threshold, they are elected, then analyze their second-choice votes * 3. update the threshold based on how many votes are left after votes from the over-threshold candidate are non-transferable (no second-choice candidate selected) * 4. redistribute the votes of each candidate who has more votes than the threshold */ export declare class Election { /** input votes */ inputBallots: Ballot[]; rounds: Round[]; availableSeats: number; transferType: TransferType; dynamicThreshold: boolean; fractionalThreshold: boolean; ballots: Ballot[]; elected: Set; eliminated: Set; NO_PREFERENCE: string; constructor( /** input votes */ inputBallots: Ballot[], { availableSeats, dynamicThreshold, transferType, }?: { availableSeats?: number; dynamicThreshold?: boolean; transferType?: TransferType; }); /** If the runner should run */ get isValid(): boolean; get errorMessage(): "No ballots were provided" | undefined; /** Deduped list of all candidates */ get allCandidates(): string[]; get links(): { source: string; target: string; value: number; }[][]; get events(): string[][]; /** Process given ballots */ run(): void; }