From 4a45e8d2e79023615e5c5a3ea4b1c6a5e5cf963f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 9 May 2023 00:26:57 +0200 Subject: [PATCH] fix: handle empty data set in median computation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils.ts | 3 +++ tests/utils.test.js | 1 + 2 files changed, 4 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index c6e78ab5..403fed95 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 eb0e7b15..21a6a7c4 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] -- 2.34.1