dhondt-calculator
v2.0.0
Published
Simple module to calculate mandates using the D'Hondt method.
Downloads
10
Maintainers
Readme
D'Hondt calculator
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 }
}