# condorcet

> TypeScript library implementing various Condorcet voting algorithms

Latest version **0.1.0** (published 2021-10-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install condorcet
pnpm add condorcet
yarn add condorcet
bun add condorcet
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2021-10-13 |
| First published | 2021-10-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 44.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Aaron Dalton |
| Maintainers | perlkonig |
| Keywords | typescript, condorcet, voting, ranking |

## Links

- npm: https://www.npmjs.com/package/condorcet
- Repository: https://github.com/Perlkonig/condorcet-ts
- Homepage: https://github.com/Perlkonig/condorcet-ts#readme
- Issues: https://github.com/Perlkonig/condorcet-ts/issues
- npm.io page: https://npm.io/package/condorcet

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2021-10-13

## README

# Condorcet Library

A TypeScript library that implements various Condorcet voting algorithms.

[Hosted by GitHub](https://github.com/Perlkonig/condorcet-ts)

## Ballots

The library exports a `Ballot` type, which is a `Map` of a key (which must be a `string` or a `number`, and which is also typed as `Candidate`) and a value (which must be a number). All the algorithms use this ballot. Typically, the magnitude of the ranks is irrelevant. All that matters is their sequence. For example, you could rank your first choice as `100` as long as your next choice was something like `110` and so on down the line. But this might differ by algorithm. Traditionally you indicate your first choice with a `1` and go from there. You do *not* need to vote for all candidates. Skipping a candidate simply means they're at the very bottom of your list.

Here's an example of a ballot ranking five candidates in the order ACBED:

```ts
const b: Ballot = new Map([
    ["A", 1],
    ["C", 2],
    ["B", 3],
    ["E", 4],
    ["D", 5]
]) as Ballot;
```

## Available Algorithms

For all algorithms, the constructor takes the list of candidates, after which you can use the `addBallot` method. You can then call the `rank` method to get a list of candidates in order, winner first.

### Schulze

A description of the method can be found on [Wikipedia](https://en.wikipedia.org/wiki/Schulze_method).

```ts
import { Candidate, Ballot, Schulze } from "condorcet";

const candidates: Set<Candidate> = new Set(["A", "B", "C", "D", "E"]);

//Build the ballot
const b: Ballot = new Map([
    ["A", 1],
    ["C", 2],
    ["B", 3],
    ["E", 4],
    ["D", 5]

]);

//Construct the Schulze object with the list of candidates
const s = new Schulze(candidates);

//Assume 50 people all voted the same way
s.addBallot(b, 50);

//Get the rankings
ranked: Candidate[] = s.rank();

//Because everybody voted unanimously, you should get the array ['A', 'C', 'B', 'E', 'D'].
//See the tests for more examples.
```

### Ranked Pairs

A description of the method can be found on [Wikipedia](https://en.wikipedia.org/wiki/Ranked_pairs).

```ts
import { Candidate, Ballot, Schulze } from "condorcet";

const candidates: Set<Candidate> = new Set(["Memphis", "Nashville", "Chattanooga", "Knoxville"]);

const b: Ballot = new Map([
    ["Chattanooga", 1],
    ["Knoxville", 2],
    ["Nashville", 3],
    ["Memphis", 4]
]);

const s = new RankedPair(candidates);
s.AddBallot(b1, 42);
const ranked: Candidate[] = s.rank();

//Because everybody voted unanimously, you should get the array ["Chattanooga", "Knoxville", "Nashville", "Memphis"].
//See the tests for more examples.
```

## TODO

* I'm an amateur programmer. Any suggestions to improve efficiency or readability would be warmly welcomed.
* I'm always looking for more test cases. I need to add code for ties.

---
_Source: https://npm.io/package/condorcet · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
