From: Jérôme Benoit Date: Mon, 8 May 2023 22:26:57 +0000 (+0200) Subject: fix: handle empty data set in median computation X-Git-Tag: v2.4.14~10 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4a45e8d2e79023615e5c5a3ea4b1c6a5e5cf963f;p=poolifier.git fix: handle empty data set in median computation Signed-off-by: Jérôme Benoit --- diff --git a/src/utils.ts b/src/utils.ts index c6e78ab5e..403fed951 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -22,6 +22,9 @@ export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions * @returns The median of the given data set. */ export const median = (dataSet: number[]): number => { + if (Array.isArray(dataSet) && dataSet.length === 0) { + return 0 + } if (Array.isArray(dataSet) && dataSet.length === 1) { return dataSet[0] } diff --git a/tests/utils.test.js b/tests/utils.test.js index eb0e7b153..21a6a7c4a 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -3,6 +3,7 @@ const { isPlainObject, median } = require('../lib/utils') describe('Utils test suite', () => { it('Verify median computation', () => { + expect(median([])).toBe(0) const array0 = [0.08] expect(median(array0)).toBe(0.08) const array1 = [0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03]