UNPKG

highest-averages

Version:

Zero-dependency and extensible set of highest averages methods for allocating seats in a parliament. ### Install ```shell npm i highest-averages ``` ### Pure Example - Define the candidates with their vote ```typescript const a = new Candidate('A', 200) c

47 lines (46 loc) 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CandidateResult = void 0; const independent_politician_1 = require("./independent_politician"); const political_party_1 = require("./political_party"); const invalid_votes_1 = require("./invalid_votes"); class CandidateResult { candidate; percentage; seat; constructor(candidate, totalVote) { this.candidate = candidate; this.percentage = (candidate.vote / totalVote) * 100; this.seat = 0; } name() { return this.candidate.name; } vote() { return this.candidate.vote; } select() { if (!this.selectable()) { throw new Error('Candidate is not selectable'); } this.seat++; } selectable() { if (!this.candidate.selectable) { return false; } if (this.candidate instanceof political_party_1.PoliticalParty) { return true; } else if (this.candidate instanceof independent_politician_1.IndependentPolitician) { return this.seat === 0; } else if (this.candidate instanceof invalid_votes_1.InvalidVotes) { return false; } else { return true; } } } exports.CandidateResult = CandidateResult;