( rows: readonly LadderRow[] | undefined, )
| 213 | * from a rung, the one further along is the one they are actually reading. |
| 214 | */ |
| 215 | export const nearestLevelUp = ( |
| 216 | rows: readonly LadderRow[] | undefined, |
| 217 | ): NearestLevelUp | null => { |
| 218 | if (!rows?.length) { |
| 219 | return null; |
| 220 | } |
| 221 | |
| 222 | let best: NearestLevelUp | null = null; |
| 223 | rows.forEach((row) => { |
| 224 | const progress = levelProgress(row.reads); |
| 225 | // Topped out: there is no next rung to walk towards. |
| 226 | if (!progress.next) { |
| 227 | return; |
| 228 | } |
| 229 | if ( |
| 230 | best && |
| 231 | (progress.toNext > best.toNext || |
| 232 | (progress.toNext === best.toNext && row.reads <= best.reads)) |
| 233 | ) { |
| 234 | return; |
| 235 | } |
| 236 | best = { ...row, ...progress, next: progress.next }; |
| 237 | }); |
| 238 | |
| 239 | return best; |
| 240 | }; |
no test coverage detected