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

@votingworks/ballot-encoder

v5.2.1

Published

Provides encoding and decoding services for completed ballots.

Downloads

37

Readme

Ballot Encoder

Provides encoding and decoding services for completed ballots.

Dev Install

To use within VS Code:

pnpm install

Pubishing New NPM Version

  1. Update the version and create a git tag: npm version [major|minor|patch]
  2. Push branch for PR review. Once approved…
  3. Generate the JavaScript files from TypeScript: pnpm prepare (or pnpx tsc)
  4. Publish current version: npm publish --access=public

Optionally, deprecate a previous version. For example: npm deprecate -f '@votingworks/[email protected]' "Poor translations"

Example

import {
  CompletedBallot,
  decodeBallot,
  electionSample as election,
  encodeBallot,
  getContests,
  vote,
} from '@votingworks/ballot-encoder'

const ballotStyle = election.ballotStyles[0]
const precinct = election.precincts[0]
const ballotId = 'abcde'
const contests = getContests({ ballotStyle, election })
const votes = vote(contests, {
  'judicial-robert-demergue': 'yes',
  'judicial-elmer-hull': 'yes',
  'question-a': 'yes',
  'question-b': 'no',
  'question-c': 'yes',
  'proposition-1': 'yes',
  'measure-101': 'no',
  '102': 'yes',
})
const ballot: CompletedBallot = {
  ballotId,
  ballotStyle,
  precinct,
  votes,
}

console.log(encodeBallot(ballot))
/*
Uint8Array [
  86, 88,  1,  2,  49,  50,  2,
  50, 51,  0, 15, 254, 208, 86,
  22, 38, 54, 70,  80
]
*/

console.log(decodeBallot(election, encodeBallot(ballot)).ballot.votes)
/*
{
  '102': 'yes',
  'judicial-robert-demergue': 'yes',
  'judicial-elmer-hull': 'yes',
  'question-a': 'yes',
  'question-b': 'no',
  'question-c': 'yes',
  'proposition-1': 'yes',
  'measure-101': 'no'
}
*/

Usage

To encode a ballot, simply pass an election and a completed ballot object to encodeBallot. You'll get back a buffer that may be stored or transmitted and later passed to decodeBallot with the same election data given to encodeBallot.

There are multiple encoder versions and by default the latest will be used when encoding. To specify the version, pass the EncoderVersion as the second parameter to encodeBallot:

import { encodeBallot, EncoderVersion } from '@votingworks/ballot-encoder'

const encodedBallot = encodeBallot(ballot, EncoderVersion.v1)

When decoding, you may pass an EncoderVersion or you may allow decodeBallot to detect the encoder version:

import { decodeBallot, EncoderVersion } from '@votingworks/ballot-encoder'

// automatically detect version
const result = decodeBallot(election, encodedBallot)
console.log('Ballot version:', result.version)
console.log('Ballot:', result.ballot)

// specify version
const result = decodeBallot(election, encodedBallot, EncoderVersion.v0)
console.log('Ballot version:', result.version)
console.log('Ballot:', result.ballot)

If you only want to detect the version, you can simply use detect:

import { detect } from '@votingworks/ballot-encoder'

const version = detect(encodedBallot)
console.log('Ballot version:', version)

Publish

This project uses the Angular Commit Message Format convention. How to publish a new version:

  1. Determine what the appropriate version bump is (i.e. patch, minor, or major). Let's assume this is stored in the environment variable BUMP.
  2. Create a branch for publishing the new version.
  3. Bump the version: npm version $BUMP.
  4. Publish the package to NPM: pnpm publish:npm.
  5. Push the branch and the new tag: git push -u origin HEAD && git push --tags.
  6. Create a pull request for your newly pushed branch. Note that this pull request should only have the version bump commit, nothing else.
  7. Get the pull request approved and merged.