-export const updateMeasurementStatistics = (
- measurementStatistics: MeasurementStatistics,
- measurementRequirements: MeasurementStatisticsRequirements,
- measurementValue: number,
- numberOfMeasurements: number
-): void => {
- if (measurementRequirements.aggregate) {
- measurementStatistics.aggregate =
- (measurementStatistics.aggregate ?? 0) + measurementValue
- measurementStatistics.minimum = Math.min(
- measurementValue,
- measurementStatistics.minimum ?? Infinity
- )
- measurementStatistics.maximum = Math.max(
- measurementValue,
- measurementStatistics.maximum ?? -Infinity
- )
- if (measurementRequirements.average && numberOfMeasurements !== 0) {
- measurementStatistics.average =
- measurementStatistics.aggregate / numberOfMeasurements
- }
- if (measurementRequirements.median && measurementValue != null) {
- measurementStatistics.history.push(measurementValue)
- measurementStatistics.median = median(measurementStatistics.history)
+export const secureRandom = (): number => {
+ return 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)
+
+/**
+ * Wraps a function so that it can only be called once.
+ *
+ * @param fn - The function to wrap.
+ * @param context - The context to bind the function to.
+ * @returns The wrapped function.
+ *
+ * @typeParam A - The function's arguments.
+ * @typeParam R - The function's return value.
+ * @typeParam C - The function's context.
+ * @internal
+ */
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export const once = <A extends any[], R, C extends ThisType<any>>(
+ fn: (...args: A) => R,
+ context: C
+): ((...args: A) => R) => {
+ let result: R
+ return (...args: A) => {
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+ if (fn != null) {
+ result = fn.apply<C, A, R>(context, args)
+ ;(fn as unknown as undefined) = (context as unknown as undefined) =
+ undefined