Skip to content

Commit

Permalink
Use for...of for minmax<T>()
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Aug 14, 2022
1 parent 0b3f023 commit 222502b
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions packages/algorithm/src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,19 @@ export function minmax<T>(
object: Iterable<T>,
fn: (first: T, second: T) => number
): [T, T] | undefined {
const it = object[Symbol.iterator]();
let item = it.next();
if (item.done) {
return undefined;
}
let vmin = item.value;
let vmax = item.value;
while (!(item = it.next()).done) {
if (fn(item.value, vmin) < 0) {
vmin = item.value;
} else if (fn(item.value, vmax) > 0) {
vmax = item.value;
let empty = true;
let vmin: T;
let vmax: T;
for (const value of object) {
if (empty) {
vmin = value;
vmax = value;
empty = false;
} else if (fn(value, vmin!) < 0) {
vmin = value;
} else if (fn(value, vmax!) > 0) {
vmax = value;
}
}
return [vmin, vmax];
return empty ? undefined : [vmin!, vmax!];
}

0 comments on commit 222502b

Please sign in to comment.