npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

single-transferable-vote

v0.0.14

Published

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

Downloads

16

Readme

Classes

Class: Ballot

A single voter's ballot, in order of preference

Constructors

constructor

new Ballot(votes, weight?): Ballot

Parameters

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

Returns

Ballot

Defined in

index.ts:26

Properties

votes

votes: string[]

Defined in

index.ts:23


weight

weight: number

Defined in

index.ts:24

Methods

toJSON

toJSON(): Object

Returns

Object

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

Defined in

index.ts:31

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:
  3. sort candidates by votes descending
  4. if a candidate has more votes than the threshold, they are elected, then analyze their second-choice votes
  5. 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)
  6. redistribute the votes of each candidate who has more votes than the threshold

Constructors

constructor

new Election(inputBallots, «destructured»?): Election

Parameters

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

Returns

Election

Defined in

index.ts:294

Properties

NO_PREFERENCE

NO_PREFERENCE: string = "No Preference"

Defined in

index.ts:292


availableSeats

availableSeats: number = 1

Defined in

index.ts:285


ballots

ballots: Ballot[] = []

Defined in

index.ts:289


dynamicThreshold

dynamicThreshold: boolean = true

Defined in

index.ts:287


elected

elected: Set<string>

Defined in

index.ts:290


eliminated

eliminated: Set<string>

Defined in

index.ts:291


fractionalThreshold

fractionalThreshold: boolean = true

Defined in

index.ts:288


inputBallots

inputBallots: Ballot[]

input votes

Defined in

index.ts:296


rounds

rounds: Round[] = []

Defined in

index.ts:284


transferType

transferType: TransferType = "fractional"

Defined in

index.ts:286

Accessors

allCandidates

get allCandidates(): string[]

Deduped list of all candidates

Returns

string[]

Defined in

index.ts:325


errorMessage

get errorMessage(): undefined | "No ballots were provided"

Returns

undefined | "No ballots were provided"

Defined in

index.ts:318


events

get events(): string[][]

Returns

string[][]

Defined in

index.ts:333


isValid

get isValid(): boolean

If the runner should run

Returns

boolean

Defined in

index.ts:314


links

get links(): { source: string ; target: string ; value: number }[][]

Returns

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

Defined in

index.ts:329

Methods

run

run(): void

Process given ballots

Returns

void

Defined in

index.ts:338

Class: Round

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

Constructors

constructor

new Round(parent?): Round

Parameters

| Name | Type | | :------ | :------ | | parent? | Election |

Returns

Round

Defined in

index.ts:122

Properties

ballots

ballots: Ballot[] = []

Input ballots

Defined in

index.ts:109


electedAtStart

electedAtStart: Set<string>

Elected candidates from the previous round

Defined in

index.ts:112


eliminatedAtStart

eliminatedAtStart: Set<string>

Eliminated candidates from the previous round

Defined in

index.ts:114


events

events: string[] = []

Log of events in this round

Defined in

index.ts:116


index

index: number = 0

Index of round in parent Election. Needed for links

Defined in

index.ts:120


links

links: { source: string ; target: string ; value: number }[] = []

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

Defined in

index.ts:118


parent

parent: Election

Defined in

index.ts:110

Accessors

candidates

get candidates(): string[]

Deduped list of candidates in the ballots

Returns

string[]

Defined in

index.ts:174


currentlyAvailableSeats

get currentlyAvailableSeats(): number

Returns

number

Defined in

index.ts:161


hopefulCandidates

get hopefulCandidates(): string[]

Returns

string[]

Defined in

index.ts:181


losers

get losers(): string[]

Returns

string[]

Defined in

index.ts:195


newWinners

get newWinners(): string[]

Candidates that surpassed the threshold in this round

Returns

string[]

Defined in

index.ts:187


sortedCandidates

get sortedCandidates(): string[]

Candidates sorted from most votes to fewest

Returns

string[]

Defined in

index.ts:178


threshold

get threshold(): number

Threshold needed to be elected in this round

Returns

number

Defined in

index.ts:165


totalActiveVotes

get totalActiveVotes(): number

Same as totalVotes, but ignores "No preference"

Returns

number

Defined in

index.ts:153


totalVotes

get totalVotes(): number

Number of votes in this round

Returns

number

Defined in

index.ts:149


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

Methods

eliminateCandidate

eliminateCandidate(candidate): Ballot[]

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

Parameters

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

Returns

Ballot[]

Defined in

index.ts:213


redistributeVotes

redistributeVotes(candidate, percentage, keep?): Ballot[]

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[]

Todo

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

Defined in

index.ts:228


toJSON

toJSON(): Object

Returns

Object

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

Defined in

index.ts:264


transferSurplus

transferSurplus(candidate, percentage): Ballot[]

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

Parameters

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

Returns

Ballot[]

Defined in

index.ts:201

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

import { Election, Ballot } from 'single-transferable-vote'

Classes

Type Aliases

Candidate

Ƭ Candidate: string

The name of a candidate

Defined in

index.ts:19


ThresholdType

Ƭ ThresholdType: "Droop" | "Hare" | "ERS97"

Defined in

index.ts:16


TransferType

Ƭ TransferType: "random" | "fractional"

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

Defined in

index.ts:15

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 | "ERS97" | | fractional | boolean | true |

Returns

number

Defined in

index.ts:49


getFlatUniqueArray

getFlatUniqueArray<T>(array): T[]

Flattens an array of simple objects (eg. strings) and makes them unique

Type parameters

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

Parameters

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

Returns

T[]

Defined in

index.ts:40


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[] | 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:

getVoterPreferencesForCandidate(
  [
    ["A", "B", "C"],
    ["A", "B", "C"],
    ["A", "C"]
  ],
"A")

output:

{
  B: 2/3,
  C: 1/3
}

Defined in

index.ts:89