From dcee41835abe4cd93a72dc902204a67e6e93bdb0 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Mon, 15 Jul 2024 16:03:53 -0400 Subject: [PATCH] fix: avoid using `for..of` where possible --- src/array/cluster.ts | 26 +++++++++++++++----------- src/array/counting.ts | 17 ++++++++++------- src/array/selectFirst.ts | 19 ++++++++++--------- src/iterable/searchIterable.ts | 2 +- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/array/cluster.ts b/src/array/cluster.ts index 75f9dd12..212cdaa3 100644 --- a/src/array/cluster.ts +++ b/src/array/cluster.ts @@ -1,4 +1,4 @@ -import { castIterable, type CastIterableItem } from 'radashi' +import { reduceIterable, type CastIterableItem } from 'radashi' /** * Splits a single list into many lists of the desired size. @@ -11,16 +11,20 @@ import { castIterable, type CastIterableItem } from 'radashi' * ``` */ export function cluster( - array: T, + iterable: T, size = 2, ): CastIterableItem[][] { - const clusters: any[][] = [] - let cluster = (clusters[0] = [] as any[]) - for (const item of castIterable(array)) { - if (cluster.length === size) { - clusters.push((cluster = [])) - } - cluster.push(item) - } - return clusters + const clusters = [[]] as any[][] + let [cluster] = clusters + return reduceIterable( + iterable, + (clusters, item) => { + if (cluster.length === size) { + clusters.push((cluster = [])) + } + cluster.push(item) + return clusters + }, + clusters, + ) } diff --git a/src/array/counting.ts b/src/array/counting.ts index 02189507..b84892a3 100644 --- a/src/array/counting.ts +++ b/src/array/counting.ts @@ -1,4 +1,4 @@ -import { castIterable, type CastIterableItem } from 'radashi' +import { reduceIterable, type CastIterableItem } from 'radashi' /** * Counts the occurrences of each unique value returned by the `identity` @@ -18,10 +18,13 @@ export function counting( if (!iterable) { return {} as Record } - const counts = {} as Record - for (const item of castIterable(iterable)) { - const id = identity(item) - counts[id] = (counts[id] ?? 0) + 1 - } - return counts + return reduceIterable( + iterable, + (counts, item) => { + const id = identity(item) + counts[id] = (counts[id] ?? 0) + 1 + return counts + }, + {} as Record, + ) } diff --git a/src/array/selectFirst.ts b/src/array/selectFirst.ts index 2c843816..9eec7545 100644 --- a/src/array/selectFirst.ts +++ b/src/array/selectFirst.ts @@ -1,4 +1,4 @@ -import { castIterable, type CastIterableItem } from 'radashi' +import { searchIterable, type CastIterableItem } from 'radashi' /** * Select performs a find + map operation, short-circuiting on the first @@ -24,16 +24,17 @@ export function selectFirst( if (!iterable) { return undefined } - let index = 0 - for (const item of castIterable(iterable)) { + let mapped: U | undefined + searchIterable(iterable, (item, index) => { if (!condition) { - const result = mapper(item, index) - if (result != null) { - return result + mapped = mapper(item, index) + if (mapped != null) { + return true } } else if (condition(item, index)) { - return mapper(item, index) + mapped = mapper(item, index) + return true } - index++ - } + }) + return mapped ?? undefined } diff --git a/src/iterable/searchIterable.ts b/src/iterable/searchIterable.ts index 82143d0e..570da163 100644 --- a/src/iterable/searchIterable.ts +++ b/src/iterable/searchIterable.ts @@ -2,7 +2,7 @@ import { castIterable, isArray, type CastIterableItem } from 'radashi' export function searchIterable( iterable: T, - match: (item: CastIterableItem, index: number) => boolean, + match: (item: CastIterableItem, index: number) => boolean | undefined, ): CastIterableItem | undefined { let item: CastIterableItem if (isArray(iterable)) {