| 1 | /** |
| 2 | * An intentional empty function. |
| 3 | */ |
| 4 | export const EMPTY_FUNCTION: () => void = Object.freeze(() => { |
| 5 | /* Intentionally empty */ |
| 6 | }) |
| 7 | |
| 8 | /** |
| 9 | * Returns the median of the given data set. |
| 10 | * |
| 11 | * @param dataSet - Data set. |
| 12 | * @returns The median of the given data set. |
| 13 | */ |
| 14 | export const median = (dataSet: number[]): number => { |
| 15 | if (Array.isArray(dataSet) && dataSet.length === 1) { |
| 16 | return dataSet[0] |
| 17 | } |
| 18 | const sortedDataSet = dataSet.slice().sort((a, b) => a - b) |
| 19 | const middleIndex = Math.floor(sortedDataSet.length / 2) |
| 20 | if (sortedDataSet.length % 2 === 0) { |
| 21 | return sortedDataSet[middleIndex / 2] |
| 22 | } |
| 23 | return (sortedDataSet[middleIndex - 1] + sortedDataSet[middleIndex]) / 2 |
| 24 | } |