From 2a66a3233d0c0938a293fa7da3c978967a6ceef9 Mon Sep 17 00:00:00 2001 From: GabrielMeyer Date: Fri, 13 Sep 2019 10:55:52 +0200 Subject: [PATCH] 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. --- .../src/app/core/ui-services/poll.service.ts | 27 ++++++++++--------- .../motions/services/motion-poll.service.ts | 13 +-------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/client/src/app/core/ui-services/poll.service.ts b/client/src/app/core/ui-services/poll.service.ts index 181b4e36b..3d11ce854 100644 --- a/client/src/app/core/ui-services/poll.service.ts +++ b/client/src/app/core/ui-services/poll.service.ts @@ -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', diff --git a/client/src/app/site/motions/services/motion-poll.service.ts b/client/src/app/site/motions/services/motion-poll.service.ts index eaf8ae99c..87e91b343 100644 --- a/client/src/app/site/motions/services/motion-poll.service.ts +++ b/client/src/app/site/motions/services/motion-poll.service.ts @@ -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; } /**