From 222502b9172d5687be446c581fb733f88fffcd37 Mon Sep 17 00:00:00 2001 From: "Afshin T. Darian" Date: Sun, 14 Aug 2022 19:30:36 +0100 Subject: [PATCH] Use for...of for minmax() --- packages/algorithm/src/find.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/algorithm/src/find.ts b/packages/algorithm/src/find.ts index 64caf1885..e6ae7ef74 100644 --- a/packages/algorithm/src/find.ts +++ b/packages/algorithm/src/find.ts @@ -220,19 +220,19 @@ export function minmax( object: Iterable, 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!]; }