From 12f8d2edb9cd8cb7ba56c30af65b3372aa242624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Tackowiak?= Date: Sat, 9 Apr 2022 11:43:34 +0200 Subject: [PATCH] fix: median function fails if it receives an empty table (#316) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * _median function fails if receive empty table. * fix: median for 1 datapoint Co-authored-by: Jérôme Wiedemann --- src/graphEntry.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/graphEntry.ts b/src/graphEntry.ts index 56c18eb..e1eb43d 100644 --- a/src/graphEntry.ts +++ b/src/graphEntry.ts @@ -513,6 +513,8 @@ export default class GraphEntry { private _median(items: EntityCachePoints) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const itemsDup = this._filterNulls([...items]).sort((a, b) => a[1]! - b[1]!); + if (itemsDup.length === 0) return null; + if (itemsDup.length === 1) return itemsDup[0][1]; const mid = Math.floor((itemsDup.length - 1) / 2); if (itemsDup.length % 2 === 1) return itemsDup[mid][1]; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion