X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils.ts;h=d85c5ba58b4988fad867a9b35f9dca5bb5bb2b45;hb=3e1f468a6b30654b2ea63f87e3de1e01e808b322;hp=46ab5051f38ff77eb2fd06c41199bd360b306276;hpb=78cea37e264d5ca527bc42eb056f3b9579a2b2c4;p=poolifier.git diff --git a/src/utils.ts b/src/utils.ts index 46ab5051..d85c5ba5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,11 +1,24 @@ /** * An intentional empty function. */ -export const EMPTY_FUNCTION: () => void = () => { +export const EMPTY_FUNCTION: () => void = Object.freeze(() => { /* Intentionally empty */ -} +}) /** - * An intentional empty object literal. + * Returns the median of the given data set. + * + * @param dataSet - Data set. + * @returns The median of the given data set. */ -export const EMPTY_LITERAL = {} +export const median = (dataSet: number[]): number => { + if (Array.isArray(dataSet) && dataSet.length === 1) { + return dataSet[0] + } + const sortedDataSet = dataSet.slice().sort((a, b) => a - b) + const middleIndex = Math.floor(sortedDataSet.length / 2) + if (sortedDataSet.length % 2 === 0) { + return sortedDataSet[middleIndex / 2] + } + return (sortedDataSet[middleIndex - 1] + sortedDataSet[middleIndex]) / 2 +}