Changes the calculation of majority-methods

- Only one method for calculation.
- Only in case of 'simple majority' a '1' will be added to the rounded result.
This commit is contained in:
GabrielMeyer 2019-09-13 10:55:52 +02:00
parent 5af32999ab
commit 2a66a3233d
2 changed files with 16 additions and 24 deletions

View File

@ -33,6 +33,18 @@ export interface MajorityMethod {
calc: (base: number) => number | null; 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 * List of available majority methods, used in motion and assignment polls
*/ */
@ -40,26 +52,17 @@ export const PollMajorityMethod: MajorityMethod[] = [
{ {
value: 'simple_majority', value: 'simple_majority',
display_name: 'Simple majority', display_name: 'Simple majority',
calc: base => { calc: base => calcMajority(base * 0.5, true)
const q = base * 0.5;
return Number.isInteger(q) ? q + 1 : Math.ceil(q);
}
}, },
{ {
value: 'two-thirds_majority', value: 'two-thirds_majority',
display_name: 'Two-thirds majority', display_name: 'Two-thirds majority',
calc: base => { calc: base => calcMajority((base / 3) * 2)
const q = (base / 3) * 2;
return Number.isInteger(q) ? q + 1 : Math.ceil(q);
}
}, },
{ {
value: 'three-quarters_majority', value: 'three-quarters_majority',
display_name: 'Three-quarters majority', display_name: 'Three-quarters majority',
calc: base => { calc: base => calcMajority((base / 4) * 3)
const q = (base / 4) * 3;
return Number.isInteger(q) ? q + 1 : Math.ceil(q);
}
}, },
{ {
value: 'disabled', value: 'disabled',

View File

@ -123,19 +123,8 @@ export class MotionPollService extends PollService {
if (!baseNumber) { if (!baseNumber) {
return undefined; return undefined;
} }
let result: number;
const calc = PollMajorityMethod.find(m => m.value === method); const calc = PollMajorityMethod.find(m => m.value === method);
if (calc && calc.calc) { return calc && calc.calc ? calc.calc(baseNumber) : null;
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;
}
} }
/** /**