dhondt-calculator

2.0.0 • Public • Published

D'Hondt calculator

build https://img.shields.io/npm/v/dhondt-calculator.svg https://img.shields.io/npm/dm/dhondt-calculator.svg

Node.js module to calculate mandates using the D'Hondt method.

Install

$ npm install dhondt-calculator@latest --save

Test

$ sudo npm install -g typescript@latest
$ npm install
$ npm test

Create an instance

/** This is the class require. */
const { Dhondt } = require('dhondt-calculator');
 
/** Some test params. */
const votes = [30, 3, 9];
const names = ['A', 'B', 'C'];
const options = {
    mandates: 3,
    blankVotes: 1,
    percentage: 2.6
};
 
/** This is the instance */
const dhondt = new Dhondt(votes, names, options);

Usage with async+await (async)

Once you have created the instance, then:

const result = await dhondt.computeWithPromise();

Usage with callback (async)

Once you have created the instance, then:

dhondt.computeWithCallback((err, result) => {});

Usage with classic promises (async)

Once you have created the instance, then:

dhondt.computeWithPromise()
  .then(result => {})
  .catch(error => {});

Usage without callback (sync)

const result = dhondt.compute();

Constructor Params

  • votes number[] the votes of each party.
  • names string[] the names of the parties.
  • options {blankVotes: number, percentage: number, mandates: number} the constructor options.

Output

  • If all went fine with the above example, it outputs:
{
    numberOfVotes: 43,
    minNumberOfVotes: 2,
    parties: { A: 3, B: 0, C: 0 }
}

Package Sidebar

Install

npm i dhondt-calculator

Weekly Downloads

8

Version

2.0.0

License

GPL-3.0

Unpacked Size

42.3 kB

Total Files

4

Last publish

Collaborators

  • jesusgn90