Merge branch 'master' of github.com:poolifier/poolifier
[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 {
86bf340d
JB
15 medRunTime: false,
16 medWaitTime: false
bbeadd16
JB
17 }
18
19/**
20 * Compute the median of the given data set.
78099a15
JB
21 *
22 * @param dataSet - Data set.
23 * @returns The median of the given data set.
24 */
25export const median = (dataSet: number[]): number => {
4a45e8d2
JB
26 if (Array.isArray(dataSet) && dataSet.length === 0) {
27 return 0
28 }
78099a15
JB
29 if (Array.isArray(dataSet) && dataSet.length === 1) {
30 return dataSet[0]
31 }
c6f42dd6
JB
32 const sortedDataSet = dataSet.slice().sort((a, b) => a - b)
33 return (
34 (sortedDataSet[(sortedDataSet.length - 1) >> 1] +
35 sortedDataSet[sortedDataSet.length >> 1]) /
36 2
37 )
78099a15 38}
0d80593b 39
3c653a03
JB
40/**
41 * Is the given object a plain object?
42 *
43 * @param obj - The object to check.
44 * @returns `true` if the given object is a plain object, `false` otherwise.
45 */
0d80593b
JB
46export const isPlainObject = (obj: unknown): boolean =>
47 typeof obj === 'object' &&
48 obj !== null &&
49 obj?.constructor === Object &&
50 Object.prototype.toString.call(obj) === '[object Object]'