chore: generate documentation
[poolifier.git] / src / utils.ts
CommitLineData
bbeadd16
JB
1import type { WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types'
2
6e9d10db
JB
3/**
4 * An intentional empty function.
5 */
4f3c3d89 6export const EMPTY_FUNCTION: () => void = Object.freeze(() => {
6e9d10db 7 /* Intentionally empty */
4f3c3d89 8})
78099a15
JB
9
10/**
bbeadd16
JB
11 * Default worker choice strategy options.
12 */
13export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions =
14 {
15 medRunTime: false
16 }
17
18/**
19 * Compute the median of the given data set.
78099a15
JB
20 *
21 * @param dataSet - Data set.
22 * @returns The median of the given data set.
23 */
24export const median = (dataSet: number[]): number => {
25 if (Array.isArray(dataSet) && dataSet.length === 1) {
26 return dataSet[0]
27 }
28 const sortedDataSet = dataSet.slice().sort((a, b) => a - b)
29 const middleIndex = Math.floor(sortedDataSet.length / 2)
30 if (sortedDataSet.length % 2 === 0) {
31 return sortedDataSet[middleIndex / 2]
32 }
33 return (sortedDataSet[middleIndex - 1] + sortedDataSet[middleIndex]) / 2
34}