Merge pull request #5019 from GabrielInTheWorld/calcPolls

Changes the calculation of majority-methods
This commit is contained in:
Emanuel Schütze 2019-09-13 11:24:42 +02:00 committed by GitHub
commit 483de8800a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 24 deletions

View File

@ -33,6 +33,18 @@ export interface MajorityMethod {
calc: (base: number) => number | null;
}
/**
* Function to round up the passed value of a poll.
*
* @param value The calculated value of 100%-base.
* @param addOne Flag, if the result should be increased by 1.
*
* @returns The necessary value to get the majority.
*/
export const calcMajority = (value: number, addOne: boolean = false) => {
return Math.ceil(value) + (addOne ? 1 : 0);
};
/**
* List of available majority methods, used in motion and assignment polls
*/
@ -40,26 +52,17 @@ export const PollMajorityMethod: MajorityMethod[] = [
{
value: 'simple_majority',
display_name: 'Simple majority',
calc: base => {
const q = base * 0.5;
return Number.isInteger(q) ? q + 1 : Math.ceil(q);
}
calc: base => calcMajority(base * 0.5, true)
},
{
value: 'two-thirds_majority',
display_name: 'Two-thirds majority',
calc: base => {
const q = (base / 3) * 2;
return Number.isInteger(q) ? q + 1 : Math.ceil(q);
}
calc: base => calcMajority((base / 3) * 2)
},
{
value: 'three-quarters_majority',
display_name: 'Three-quarters majority',
calc: base => {
const q = (base / 4) * 3;
return Number.isInteger(q) ? q + 1 : Math.ceil(q);
}
calc: base => calcMajority((base / 4) * 3)
},
{
value: 'disabled',

View File

@ -123,19 +123,8 @@ export class MotionPollService extends PollService {
if (!baseNumber) {
return undefined;
}
let result: number;
const calc = PollMajorityMethod.find(m => m.value === method);
if (calc && calc.calc) {
result = calc.calc(baseNumber);
} else {
result = null;
}
// rounding up, or if a integer was hit, adding one.
if (result % 1 !== 0) {
return Math.ceil(result);
} else {
return result + 1;
}
return calc && calc.calc ? calc.calc(baseNumber) : null;
}
/**