From d159d439ced5af450a46ca40db1791658cfca8e9 Mon Sep 17 00:00:00 2001 From: Pithon Kabiro <pithon.kabiro@hft-stuttgart.de> Date: Fri, 22 Oct 2021 18:51:05 +0200 Subject: [PATCH] New function: calculate index of start timestamp --- public/js/src_modules/aggregateHelpers.mjs | 25 ++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/public/js/src_modules/aggregateHelpers.mjs b/public/js/src_modules/aggregateHelpers.mjs index 85c1583..a79dd97 100644 --- a/public/js/src_modules/aggregateHelpers.mjs +++ b/public/js/src_modules/aggregateHelpers.mjs @@ -60,19 +60,36 @@ const checkIfLeapYear = function (year) { * @returns {Number} An integer representing the index of the timestamp of interest in the array of timestamps */ const getIndexOfTimestamp = function (inputTimestampArr, timestampOfInterest) { - const timestampIndex = inputTimestampArr.findIndex( + return inputTimestampArr.findIndex( (timestamp) => timestamp === timestampOfInterest ); +}; + +/** + * Calculate the index of a start 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 getIndexOfStartTimestamp = function ( + inputTimestampArr, + timestampOfInterest +) { + const timestampStartIndex = getIndexOfTimestamp( + inputTimestampArr, + timestampOfInterest + ); // If the timestamp does not exist in the timestamp array - if (timestampIndex === -1) { + if (timestampStartIndex === -1) { throw new Error( - "A start or end timestamp could not be found in the timestamp array" + "A start timestamp could not be found in the timestamp array" ); } // If the timestamp exists in the timestamp array else { - return timestampIndex; + return timestampStartIndex; } }; -- GitLab