refactor: add more sanity checks to charging profiles handling code
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index ff41faf8a674540a8b512ac8664cb61e98bfc6ec..48d0587cbce16e9db798c48877795325e8088eff 100644 (file)
@@ -262,11 +262,12 @@ export const insertAt = (str: string, subStr: string, pos: number): string =>
  * Computes the retry delay in milliseconds using an exponential backoff algorithm.
  *
  * @param retryNumber - the number of retries that have already been attempted
+ * @param maxDelayRatio - the maximum ratio of the delay that can be randomized
  * @returns delay in milliseconds
  */
 export const exponentialDelay = (retryNumber = 0, maxDelayRatio = 0.2): number => {
   const delay = Math.pow(2, retryNumber) * 100;
-  const randomSum = delay * maxDelayRatio * secureRandom(); // 0-20% of the delay
+  const randomSum = delay * maxDelayRatio * secureRandom(); // 0-(maxDelayRatio*100)% of the delay
   return delay + randomSum;
 };
 
@@ -354,3 +355,12 @@ export const getWebSocketCloseEventStatusString = (code: number): string => {
   }
   return '(Unknown)';
 };
+
+export const isArraySorted = <T>(array: T[], compareFn: (a: T, b: T) => number): boolean => {
+  for (let index = 0; index < array.length - 1; ++index) {
+    if (compareFn(array[index], array[index + 1]) > 0) {
+      return false;
+    }
+  }
+  return true;
+};