(items: readonly T[], valueSelector: (item: T) => number)
| 84 | } |
| 85 | |
| 86 | export function rankPercentiles<T>(items: readonly T[], valueSelector: (item: T) => number): Map<T, number> { |
| 87 | const result = new Map<T, number>() |
| 88 | if (items.length === 0) return result |
| 89 | |
| 90 | const values = items.map((item) => ({ item, value: nonNegative(valueSelector(item)) })) |
| 91 | const min = Math.min(...values.map((entry) => entry.value)) |
| 92 | const max = Math.max(...values.map((entry) => entry.value)) |
| 93 | |
| 94 | if (min === max) { |
| 95 | const percentile = max > 0 ? 1 : 0 |
| 96 | for (const entry of values) result.set(entry.item, percentile) |
| 97 | return result |
| 98 | } |
| 99 | |
| 100 | const sorted = [...values].sort((a, b) => a.value - b.value) |
| 101 | let index = 0 |
| 102 | while (index < sorted.length) { |
| 103 | let end = index |
| 104 | while (end + 1 < sorted.length && sorted[end + 1].value === sorted[index].value) end++ |
| 105 | const percentile = (index + end) / 2 / (sorted.length - 1) |
| 106 | for (let i = index; i <= end; i++) result.set(sorted[i].item, percentile) |
| 107 | index = end + 1 |
| 108 | } |
| 109 | |
| 110 | return result |
| 111 | } |
| 112 | |
| 113 | export function computePrivateRegularity(activeMonths: readonly string[]): number { |
| 114 | const monthIndexes = getActiveMonthIndexes(activeMonths) |
no test coverage detected