+
+/**
+ * Detects whether the given value is a kill behavior or not.
+ *
+ * @typeParam KB - Which specific KillBehavior type to test against.
+ * @param killBehavior - Which kind of kill behavior to detect.
+ * @param value - Any value.
+ * @returns `true` if `value` was strictly equals to `killBehavior`, otherwise `false`.
+ * @internal
+ */
+export const isKillBehavior = <KB extends KillBehavior>(
+ killBehavior: KB,
+ value: unknown
+): value is KB => {
+ return value === killBehavior
+}
+
+/**
+ * Detects whether the given value is an asynchronous function or not.
+ *
+ * @param fn - Any value.
+ * @returns `true` if `fn` was an asynchronous function, otherwise `false`.
+ * @internal
+ */
+export const isAsyncFunction = (
+ fn: unknown
+): fn is (...args: unknown[]) => Promise<unknown> => {
+ return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction'
+}
+
+/**
+ * Updates the given measurement statistics.
+ *
+ * @param measurementStatistics - The measurement statistics to update.
+ * @param measurementRequirements - The measurement statistics requirements.
+ * @param measurementValue - The measurement value.
+ * @param numberOfMeasurements - The number of measurements.
+ * @internal
+ */
+export const updateMeasurementStatistics = (
+ measurementStatistics: MeasurementStatistics,
+ measurementRequirements: MeasurementStatisticsRequirements,
+ measurementValue: number
+): void => {
+ if (measurementRequirements.aggregate) {
+ measurementStatistics.aggregate =
+ (measurementStatistics.aggregate ?? 0) + measurementValue
+ measurementStatistics.minimum = min(
+ measurementValue,
+ measurementStatistics.minimum ?? Infinity
+ )
+ measurementStatistics.maximum = max(
+ measurementValue,
+ measurementStatistics.maximum ?? -Infinity
+ )
+ if (
+ (measurementRequirements.average || measurementRequirements.median) &&
+ measurementValue != null
+ ) {
+ 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
+ }
+ }
+ }
+}
+
+/**
+ * Generates a cryptographically secure random number in the [0,1[ range
+ *
+ * @returns A number in the [0,1[ range
+ * @internal
+ */
+export const secureRandom = (): number => {
+ return webcrypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000
+}
+
+/**
+ * Returns the minimum of the given numbers.
+ * If no numbers are given, `Infinity` is returned.
+ *
+ * @param args - The numbers to get the minimum of.
+ * @returns The minimum of the given numbers.
+ * @internal
+ */
+export const min = (...args: number[]): number =>
+ args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity)
+
+/**
+ * Returns the maximum of the given numbers.
+ * If no numbers are given, `-Infinity` is returned.
+ *
+ * @param args - The numbers to get the maximum of.
+ * @returns The maximum of the given numbers.
+ * @internal
+ */
+export const max = (...args: number[]): number =>
+ args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity)