15 lines
381 B
TypeScript
15 lines
381 B
TypeScript
export function maxByOrNull<T>(value: Iterable<T>, transform: (it: T) => number): T | null {
|
|
let maxIt!: T;
|
|
let max!: number;
|
|
let found = false;
|
|
for (const it of value) {
|
|
const val = transform(it);
|
|
if (found && val <= max) continue;
|
|
maxIt = it;
|
|
max = val;
|
|
found = true;
|
|
}
|
|
if (!found) return null;
|
|
return maxIt;
|
|
}
|