From bee78fb8dc9761cae7b27e01b8dc092f2f86bbaf Mon Sep 17 00:00:00 2001
From: Pithon Kabiro <pithon.kabiro@hft-stuttgart.de>
Date: Fri, 22 Oct 2021 18:52:43 +0200
Subject: [PATCH] New function: calculate index of end timestamp

---
 public/js/src_modules/aggregateHelpers.mjs | 32 ++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/public/js/src_modules/aggregateHelpers.mjs b/public/js/src_modules/aggregateHelpers.mjs
index a79dd97..49f2ac9 100644
--- a/public/js/src_modules/aggregateHelpers.mjs
+++ b/public/js/src_modules/aggregateHelpers.mjs
@@ -93,6 +93,34 @@ const getIndexOfStartTimestamp = function (
   }
 };
 
+/**
+ * Calculate the index of an end timestamp in an array of timestamps
+ *
+ * @param {Array} inputTimestampArr An array of timestamps, extracted from an array of observations
+ * @param {String} timestampOfInterest A string representing the timestamp of interest in ISO 8601 format
+ * @returns {Number} An integer representing the index of the timestamp of interest in the array of timestamps
+ */
+const getIndexOfEndTimestamp = function (
+  inputTimestampArr,
+  timestampOfInterest
+) {
+  const timestampEndIndex = getIndexOfTimestamp(
+    inputTimestampArr,
+    timestampOfInterest
+  );
+
+  // If the timestamp does not exist in the timestamp array
+  if (timestampEndIndex === -1) {
+    throw new Error(
+      "An end timestamp could not be found in the timestamp array"
+    );
+  }
+  // If the timestamp exists in the timestamp array
+  else {
+    return timestampEndIndex;
+  }
+};
+
 /**
  * Calculate the indexes of the start and end timestamps
  * @param {Array} obsTimestampArr An array of observations timestamps
@@ -122,11 +150,11 @@ const calculateIndexStartEndTimestamp = function (
   );
 
   // Calculate the indexes of the timestamps for the start and end of interval
-  const indexStartTimestamp = getIndexOfTimestamp(
+  const indexStartTimestamp = getIndexOfStartTimestamp(
     obsTimestampArr,
     startIso8601DateTimeString
   );
-  const indexEndTimestamp = getIndexOfTimestamp(
+  const indexEndTimestamp = getIndexOfEndTimestamp(
     obsTimestampArr,
     endIso8601DateTimeString
   );
-- 
GitLab