<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Classes](#classes)
  - [Class: Ballot](#class-ballot)
    - [Constructors](#constructors)
    - [Properties](#properties)
    - [Methods](#methods)
  - [Class: Election](#class-election)
    - [Constructors](#constructors-1)
    - [Properties](#properties-1)
    - [Accessors](#accessors)
    - [Methods](#methods-1)
  - [Class: Round](#class-round)
    - [Constructors](#constructors-2)
    - [Properties](#properties-2)
    - [Accessors](#accessors-1)
    - [Methods](#methods-2)
- [single-transferable-vote - v0.0.8](#single-transferable-vote---v008)
  - [Installation](#installation)
  - [Usage](#usage)
  - [Classes](#classes-1)
  - [Type Aliases](#type-aliases)
    - [Candidate](#candidate)
    - [ThresholdType](#thresholdtype)
    - [TransferType](#transfertype)
  - [Functions](#functions)
    - [calculateThreshold](#calculatethreshold)
    - [getFlatUniqueArray](#getflatuniquearray)
    - [getVoterPreferencesForCandidate](#getvoterpreferencesforcandidate)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# Classes


<a name="classesballotmd"></a>

## Class: Ballot

A single voter's ballot, in order of preference

### Constructors

#### constructor

• **new Ballot**(`votes`, `weight?`): [`Ballot`](#classesballotmd)

##### Parameters

| Name | Type | Default value |
| :------ | :------ | :------ |
| `votes` | `string`[] | `undefined` |
| `weight` | `number` | `1` |

##### Returns

[`Ballot`](#classesballotmd)

##### Defined in

[index.ts:26](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L26)

### Properties

#### votes

• **votes**: `string`[]

##### Defined in

[index.ts:23](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L23)

___

#### weight

• **weight**: `number`

##### Defined in

[index.ts:24](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L24)

### Methods

#### toJSON

▸ **toJSON**(): `Object`

##### Returns

`Object`

| Name | Type |
| :------ | :------ |
| `votes` | `string`[] |
| `weight` | `number` |

##### Defined in

[index.ts:31](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L31)


<a name="classeselectionmd"></a>

## Class: Election

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

### Constructors

#### constructor

• **new Election**(`inputBallots`, `«destructured»?`): [`Election`](#classeselectionmd)

##### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `inputBallots` | [`Ballot`](#classesballotmd)[] | input votes |
| `«destructured»` | `Object` | - |
| › `availableSeats?` | `number` | - |
| › `dynamicThreshold?` | `boolean` | - |
| › `transferType?` | [`TransferType`](#transfertype) | - |

##### Returns

[`Election`](#classeselectionmd)

##### Defined in

[index.ts:294](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L294)

### Properties

#### NO\_PREFERENCE

• **NO\_PREFERENCE**: `string` = `"No Preference"`

##### Defined in

[index.ts:292](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L292)

___

#### availableSeats

• **availableSeats**: `number` = `1`

##### Defined in

[index.ts:285](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L285)

___

#### ballots

• **ballots**: [`Ballot`](#classesballotmd)[] = `[]`

##### Defined in

[index.ts:289](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L289)

___

#### dynamicThreshold

• **dynamicThreshold**: `boolean` = `true`

##### Defined in

[index.ts:287](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L287)

___

#### elected

• **elected**: `Set`\<`string`\>

##### Defined in

[index.ts:290](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L290)

___

#### eliminated

• **eliminated**: `Set`\<`string`\>

##### Defined in

[index.ts:291](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L291)

___

#### fractionalThreshold

• **fractionalThreshold**: `boolean` = `true`

##### Defined in

[index.ts:288](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L288)

___

#### inputBallots

• **inputBallots**: [`Ballot`](#classesballotmd)[]

input votes

##### Defined in

[index.ts:296](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L296)

___

#### rounds

• **rounds**: [`Round`](#classesroundmd)[] = `[]`

##### Defined in

[index.ts:284](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L284)

___

#### transferType

• **transferType**: [`TransferType`](#transfertype) = `"fractional"`

##### Defined in

[index.ts:286](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L286)

### Accessors

#### allCandidates

• `get` **allCandidates**(): `string`[]

Deduped list of all candidates

##### Returns

`string`[]

##### Defined in

[index.ts:325](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L325)

___

#### errorMessage

• `get` **errorMessage**(): `undefined` \| ``"No ballots were provided"``

##### Returns

`undefined` \| ``"No ballots were provided"``

##### Defined in

[index.ts:318](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L318)

___

#### events

• `get` **events**(): `string`[][]

##### Returns

`string`[][]

##### Defined in

[index.ts:333](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L333)

___

#### isValid

• `get` **isValid**(): `boolean`

If the runner should run

##### Returns

`boolean`

##### Defined in

[index.ts:314](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L314)

___

#### links

• `get` **links**(): \{ `source`: `string` ; `target`: `string` ; `value`: `number`  }[][]

##### Returns

\{ `source`: `string` ; `target`: `string` ; `value`: `number`  }[][]

##### Defined in

[index.ts:329](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L329)

### Methods

#### run

▸ **run**(): `void`

Process given ballots

##### Returns

`void`

##### Defined in

[index.ts:338](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L338)


<a name="classesroundmd"></a>

## Class: Round

A single round's result, including active candidates, total votes, and the winner

### Constructors

#### constructor

• **new Round**(`parent?`): [`Round`](#classesroundmd)

##### Parameters

| Name | Type |
| :------ | :------ |
| `parent?` | [`Election`](#classeselectionmd) |

##### Returns

[`Round`](#classesroundmd)

##### Defined in

[index.ts:122](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L122)

### Properties

#### ballots

• **ballots**: [`Ballot`](#classesballotmd)[] = `[]`

Input ballots

##### Defined in

[index.ts:109](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L109)

___

#### electedAtStart

• **electedAtStart**: `Set`\<`string`\>

Elected candidates from the previous round

##### Defined in

[index.ts:112](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L112)

___

#### eliminatedAtStart

• **eliminatedAtStart**: `Set`\<`string`\>

Eliminated candidates from the previous round

##### Defined in

[index.ts:114](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L114)

___

#### events

• **events**: `string`[] = `[]`

Log of events in this round

##### Defined in

[index.ts:116](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L116)

___

#### index

• **index**: `number` = `0`

Index of round in parent `Election`. Needed for `links`

##### Defined in

[index.ts:120](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L120)

___

#### links

• **links**: \{ `source`: `string` ; `target`: `string` ; `value`: `number`  }[] = `[]`

Links between candidates in this round. Useful for sankey visualization.

##### Defined in

[index.ts:118](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L118)

___

#### parent

• **parent**: [`Election`](#classeselectionmd)

##### Defined in

[index.ts:110](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L110)

### Accessors

#### candidates

• `get` **candidates**(): `string`[]

Deduped list of candidates in the ballots

##### Returns

`string`[]

##### Defined in

[index.ts:174](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L174)

___

#### currentlyAvailableSeats

• `get` **currentlyAvailableSeats**(): `number`

##### Returns

`number`

##### Defined in

[index.ts:161](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L161)

___

#### hopefulCandidates

• `get` **hopefulCandidates**(): `string`[]

##### Returns

`string`[]

##### Defined in

[index.ts:181](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L181)

___

#### losers

• `get` **losers**(): `string`[]

##### Returns

`string`[]

##### Defined in

[index.ts:195](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L195)

___

#### newWinners

• `get` **newWinners**(): `string`[]

Candidates that surpassed the threshold in this round

##### Returns

`string`[]

##### Defined in

[index.ts:187](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L187)

___

#### sortedCandidates

• `get` **sortedCandidates**(): `string`[]

Candidates sorted from most votes to fewest

##### Returns

`string`[]

##### Defined in

[index.ts:178](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L178)

___

#### threshold

• `get` **threshold**(): `number`

Threshold needed to be elected in this round

##### Returns

`number`

##### Defined in

[index.ts:165](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L165)

___

#### totalActiveVotes

• `get` **totalActiveVotes**(): `number`

Same as `totalVotes`, but ignores "No preference"

##### Returns

`number`

##### Defined in

[index.ts:153](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L153)

___

#### totalVotes

• `get` **totalVotes**(): `number`

Number of votes in this round

##### Returns

`number`

##### Defined in

[index.ts:149](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L149)

___

#### votes

• `get` **votes**(): `Object`

Calculate votes by adding the current this.ballots[i].votes[0] * this.ballots[i].weight

##### Returns

`Object`

##### Defined in

[index.ts:131](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L131)

### Methods

#### eliminateCandidate

▸ **eliminateCandidate**(`candidate`): [`Ballot`](#classesballotmd)[]

Removes a candidate from subsequent rounds and redistributes all of their votes

##### Parameters

| Name | Type |
| :------ | :------ |
| `candidate` | `string` |

##### Returns

[`Ballot`](#classesballotmd)[]

##### Defined in

[index.ts:213](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L213)

___

#### redistributeVotes

▸ **redistributeVotes**(`candidate`, `percentage`, `keep?`): [`Ballot`](#classesballotmd)[]

for ballots that have the candidate in 1st, splice out the first item and redistribute the votes with weighting

##### Parameters

| Name | Type | Default value |
| :------ | :------ | :------ |
| `candidate` | `string` | `undefined` |
| `percentage` | `number` | `undefined` |
| `keep` | `boolean` | `false` |

##### Returns

[`Ballot`](#classesballotmd)[]

**`Todo`**

prevent votes from being redistributed to the an already-winning candidate

##### Defined in

[index.ts:228](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L228)

___

#### toJSON

▸ **toJSON**(): `Object`

##### Returns

`Object`

| Name | Type |
| :------ | :------ |
| `total` | `number` |
| `votes` | \{ `[candidate: string]`: `number`;  } |

##### Defined in

[index.ts:264](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L264)

___

#### transferSurplus

▸ **transferSurplus**(`candidate`, `percentage`): [`Ballot`](#classesballotmd)[]

Transfers votes from a winning candidate to their voters' next preference

##### Parameters

| Name | Type |
| :------ | :------ |
| `candidate` | `string` |
| `percentage` | `number` |

##### Returns

[`Ballot`](#classesballotmd)[]

##### Defined in

[index.ts:201](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L201)


<a name="modulesmd"></a>

# single-transferable-vote - v0.0.8

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'
```

## Classes

- [Ballot](#classesballotmd)
- [Election](#classeselectionmd)
- [Round](#classesroundmd)

## Type Aliases

### Candidate

Ƭ **Candidate**: `string`

The name of a candidate

#### Defined in

[index.ts:19](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L19)

___

### ThresholdType

Ƭ **ThresholdType**: ``"Droop"`` \| ``"Hare"`` \| ``"ERS97"``

#### Defined in

[index.ts:16](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L16)

___

### TransferType

Ƭ **TransferType**: ``"random"`` \| ``"fractional"``

Fractional is deterministic, while random or other sampling methods could unfairly weight certain votes.

#### Defined in

[index.ts:15](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L15)

## Functions

### calculateThreshold

▸ **calculateThreshold**(`ballotCount`, `seats`, `type?`, `fractional?`): `number`

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.

#### Parameters

| Name | Type | Default value |
| :------ | :------ | :------ |
| `ballotCount` | `number` | `undefined` |
| `seats` | `number` | `undefined` |
| `type` | [`ThresholdType`](#thresholdtype) | `"ERS97"` |
| `fractional` | `boolean` | `true` |

#### Returns

`number`

#### Defined in

[index.ts:49](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L49)

___

### getFlatUniqueArray

▸ **getFlatUniqueArray**\<`T`\>(`array`): `T`[]

Flattens an array of simple objects (eg. `string`s) and makes them unique

#### Type parameters

| Name |
| :------ |
| `T` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `array` | `T`[][] |

#### Returns

`T`[]

#### Defined in

[index.ts:40](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L40)

___

### getVoterPreferencesForCandidate

▸ **getVoterPreferencesForCandidate**(`ballots`, `candidate`, `roundIndex?`): `Object`

Gets next preferences for a candidate from a list of votes.

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `ballots` | [`Ballot`](#classesballotmd)[] | `undefined` | All votes |
| `candidate` | `string` | `undefined` | Candidate to analyze next preference for |
| `roundIndex` | `number` | `0` | Current round to filter results by |

#### Returns

`Object`

**`Example`**

input:
```ts
getVoterPreferencesForCandidate(
  [
    ["A", "B", "C"],
    ["A", "B", "C"],
    ["A", "C"]
  ],
"A")
```
output:
```ts
{
  B: 2/3,
  C: 1/3
}
```

#### Defined in

[index.ts:89](https://github.com/danielgamage/instant-runoff-tabulator/blob/2dbc73d9f4b678980b712d08b2fcf11090d6a741/src/index.ts#L89)
