Skip to content

Iteration Generators

An iteration generator is a generator that iterates over an iterable and passes through in a callback function at least the current value and an object called the iteration info. Which is and object that gives away info about the current iteration of the object. How many iterations there are going to be. All of them are generators. Some async some not They are created to be well typed.

Iteration Info

The iteration info is an object that is created to pass on information about the current iteration.

Iterate

iterate<T HasForEachMethod | Generator, U>(
iterable:T, (value:unknown: info:IterationInfo, key:unknown)=> U
):AsyncGenerator

A function that takes in either a generator or a iterable with a forEach()
as the first parameter and a callback as the second. It iterates through the first parameter and passes through it's value, the Iteration Info. If the first parameter is a iterable with a forEach() the key as the last parameter.

Usage in Astro

{iterate([1,2,3], (value:number, info:IterationInfo) => (
<div style={{backgroundColor: info.isOdd ? 'red': 'blue' }} >
{value}
</div>
))}

Sync Iterate

syncIterate<T HasForEachMethod | Generator, U>(
iterable:T, (value:unknown: info:IterationInfo, key:unknown)=> U
):Generator

A function that takes in either a generator or a iterable with a forEach()
as the first parameter and a callback as the second. It iterates through the first parameter and passes through it's value, the Iteration Info. If the first parameter is a iterable with a forEach() the key as the last parameter.

Usage in React

{Array.from(syncIterate([1,2,3], (value:number, info:IterationInfo) => (
<div style={{backgroundColor: info.isOdd ? 'red': 'blue' }} >
{value}
</div>
)))}

Usage in Vue

<script setup lang="ts">
const generatedNumberAndIterationInfo =
syncIterate(
[1,2,3],
(value:number, info:IterationInfo) => [value,info])
)
</script>
<div
v-for="[value, info] in generatedNumberAndIterationInfo"
style={{backgroundColor: info.isOdd ? 'red': 'blue' }}
>
{{value}}
</div>