(numbers: number[])
| 27 | @returns function to calculate heat index of a given number |
| 28 | */ |
| 29 | export function createHeatIndexFunction(numbers: number[]): (value: number) => number { |
| 30 | const steps = 10; // GH has 10 heat colors |
| 31 | const min = Math.min(...numbers); |
| 32 | const max = Math.max(...numbers); |
| 33 | |
| 34 | return (value: number) => { |
| 35 | // Inverse linear interpolation figures out how far the value is between min & max |
| 36 | const interp = Math.max(0, Math.min(1, inverseLinearInterpolation(min, max, value))); |
| 37 | |
| 38 | // Maps the [0.0, 1.0] value to [steps, 1] |
| 39 | const floored = Math.floor(interp * steps); |
| 40 | const heatIndex = Math.max(1, steps - floored); |
| 41 | |
| 42 | return heatIndex; |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | export function randomArrayItem<T>(items: T[]): T { |
| 47 | return items.at(Math.floor(Math.random() * items.length))!; |
no test coverage detected