build(deps): bump poolifier
[poolifier.git] / src / utils.ts
index 21b2032b47086484e2b39475b06310950d95c08b..e1d6c11859936dfef95d5e4777e509ca50df5f0f 100644 (file)
@@ -59,14 +59,48 @@ export const availableParallelism = (): number => {
   try {
     availableParallelism = os.availableParallelism()
   } catch {
-    const numberOfCpus = os.cpus()
-    if (Array.isArray(numberOfCpus) && numberOfCpus.length > 0) {
-      availableParallelism = numberOfCpus.length
+    const cpus = os.cpus()
+    if (Array.isArray(cpus) && cpus.length > 0) {
+      availableParallelism = cpus.length
     }
   }
   return availableParallelism
 }
 
+/**
+ * Returns the worker type of the given worker.
+ *
+ * @param worker - The worker to get the type of.
+ * @returns The worker type of the given worker.
+ * @internal
+ */
+export const getWorkerType = <Worker extends IWorker>(
+  worker: Worker
+): WorkerType | undefined => {
+  if (worker instanceof ThreadWorker) {
+    return WorkerTypes.thread
+  } else if (worker instanceof ClusterWorker) {
+    return WorkerTypes.cluster
+  }
+}
+
+/**
+ * Returns the worker id of the given worker.
+ *
+ * @param worker - The worker to get the id of.
+ * @returns The worker id of the given worker.
+ * @internal
+ */
+export const getWorkerId = <Worker extends IWorker>(
+  worker: Worker
+): number | undefined => {
+  if (worker instanceof ThreadWorker) {
+    return worker.threadId
+  } else if (worker instanceof ClusterWorker) {
+    return worker.id
+  }
+}
+
 /**
  * Sleeps for the given amount of milliseconds.
  *
@@ -116,41 +150,6 @@ export const average = (dataSet: number[]): number => {
   )
 }
 
-/**
- * Returns the worker type of the given worker.
- *
- * @param worker - The worker to get the type of.
- * @returns The worker type of the given worker.
- * @internal
- */
-export const getWorkerType = <Worker extends IWorker>(
-  worker: Worker
-): WorkerType | undefined => {
-  if (worker instanceof ThreadWorker) {
-    return WorkerTypes.thread
-  }
-  if (worker instanceof ClusterWorker) {
-    return WorkerTypes.cluster
-  }
-}
-
-/**
- * Returns the worker id of the given worker.
- *
- * @param worker - The worker to get the id of.
- * @returns The worker id of the given worker.
- * @internal
- */
-export const getWorkerId = <Worker extends IWorker>(
-  worker: Worker
-): number | undefined => {
-  if (worker instanceof ThreadWorker) {
-    return worker.threadId
-  } else if (worker instanceof ClusterWorker) {
-    return worker.id
-  }
-}
-
 /**
  * Computes the median of the given data set.
  *
@@ -258,38 +257,18 @@ export const updateMeasurementStatistics = (
       measurementStatistics.history.push(measurementValue)
       if (measurementRequirements.average) {
         measurementStatistics.average = average(measurementStatistics.history)
+      } else if (measurementStatistics.average != null) {
+        delete measurementStatistics.average
       }
       if (measurementRequirements.median) {
         measurementStatistics.median = median(measurementStatistics.history)
+      } else if (measurementStatistics.median != null) {
+        delete measurementStatistics.median
       }
     }
   }
 }
 
-/**
- * Executes a function once at a time.
- *
- * @param fn - The function to execute.
- * @param context - The context to bind the function to.
- * @returns The function to execute.
- */
-export const once = (
-  // eslint-disable-next-line @typescript-eslint/no-explicit-any
-  fn: (...args: any[]) => void,
-  context: unknown
-  // eslint-disable-next-line @typescript-eslint/no-explicit-any
-): ((...args: any[]) => void) => {
-  let called = false
-  // eslint-disable-next-line @typescript-eslint/no-explicit-any
-  return function (...args: any[]): void {
-    if (!called) {
-      called = true
-      fn.apply(context, args)
-      called = false
-    }
-  }
-}
-
 /**
  * Generate a cryptographically secure random number in the [0,1[ range
  *