Skip to content

Commit

Permalink
fix: avoid using for..of where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jul 15, 2024
1 parent 2ee59d3 commit dcee418
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
26 changes: 15 additions & 11 deletions src/array/cluster.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,16 +11,20 @@ import { castIterable, type CastIterableItem } from 'radashi'
* ```
*/
export function cluster<T extends object>(
array: T,
iterable: T,
size = 2,
): CastIterableItem<T>[][] {
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,
)
}
17 changes: 10 additions & 7 deletions src/array/counting.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -18,10 +18,13 @@ export function counting<T extends object, K extends keyof any>(
if (!iterable) {
return {} as Record<K, number>
}
const counts = {} as Record<K, number>
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<K, number>,
)
}
19 changes: 10 additions & 9 deletions src/array/selectFirst.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,16 +24,17 @@ export function selectFirst<T extends object, U>(
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
}
2 changes: 1 addition & 1 deletion src/iterable/searchIterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { castIterable, isArray, type CastIterableItem } from 'radashi'

export function searchIterable<T extends object>(
iterable: T,
match: (item: CastIterableItem<T>, index: number) => boolean,
match: (item: CastIterableItem<T>, index: number) => boolean | undefined,
): CastIterableItem<T> | undefined {
let item: CastIterableItem<T>
if (isArray(iterable)) {
Expand Down

0 comments on commit dcee418

Please sign in to comment.