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

condorcet

v0.1.0

Published

TypeScript library implementing various Condorcet voting algorithms

Downloads

5

Readme

Condorcet Library

A TypeScript library that implements various Condorcet voting algorithms.

Hosted by GitHub

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:

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.

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.

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.