function enumerate
enumerate<T>(iter?:
AsyncIterable<T>
| Iterable<T>
| null
): Enumerable<T>

Create an Enumerable from any iterable or async iterable.

This is the factory function for creating Enumerable instances. It provides a fluent API for working with async data streams, making it easy to chain operations like map, filter, and transform.

Important: This function wraps an iterable but does NOT add index counters. To add index counters, call .enum() on the returned Enumerable.

Why use Enumerable?

  • Composable operations via method chaining
  • Works seamlessly with async data
  • Integrates with process I/O
  • Lazy evaluation for memory efficiency
  • Type-safe transformations

Examples

Convert array to AsyncIterable

import { enumerate } from "jsr:@j50n/proc";

const result = await enumerate([1, 2, 3]).collect();
// [1, 2, 3]

Add index counters with .enum()

import { enumerate } from "jsr:@j50n/proc";

// .enum() adds [item, index] tuples
const result = await enumerate(["a", "b", "c"])
  .enum()
  .map(([item, i]) => `${i}: ${item}`)
  .collect();
// ["0: a", "1: b", "2: c"]

Use with for await

import { enumerate } from "jsr:@j50n/proc";

for await (const n of enumerate([1, 2, 3])) {
  console.log(n);
}

Type Parameters

Parameters

optional
iter:
AsyncIterable<T>
| Iterable<T>
| null

An Iterable, AsyncIterable, or null/undefined (treated as empty).

Return Type

An Enumerable wrapping the input.