Iterall: JavaScript Iterables for All Environments

1.3.0 · active · verified Sun Apr 19

Iterall is a minimalist, zero-dependency utility library designed to enable the use of JavaScript Iterables and AsyncIterables across all JavaScript environments, including older versions of Internet Explorer. It provides crucial helpers for libraries that wish to accept various iterable inputs (like Arrays, Maps, Sets, NodeLists, TypedArrays, or custom data structures) instead of being limited to only Arrays. The current stable version is 1.3.0, and the project maintains a steady release cadence with improvements to TypeScript definitions and async iterator support. Its key differentiators include its tiny footprint (under 1KB gzipped), its 'library for libraries' approach, and its robust compatibility with both modern and legacy JavaScript runtimes through its intelligent fallback mechanism for `Symbol.iterator`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `isCollection` to check for iterable/array-like structures and `forEach` for synchronous iteration, along with `forAwaitEach` for handling asynchronous iterables, all compatible across environments.

import { isCollection, forEach, forAwaitEach } from 'iterall';

// Example 1: Handling various synchronous collections
function processCollection(data) {
  if (isCollection(data)) {
    forEach(data, (item, index) => {
      console.log(`Sync Item ${index}: ${item}`);
    });
    console.log('Finished synchronous iteration.');
  } else {
    console.log('Input is not a recognized collection.');
  }
}

processCollection([1, 2, 3]);
processCollection(new Set(['a', 'b']));

// Example 2: Handling asynchronous iterables
async function* asyncGenerator() {
  yield 'hello';
  await new Promise(resolve => setTimeout(resolve, 100));
  yield 'world';
}

async function processAsyncIterable(asyncData) {
  console.log('Starting asynchronous iteration...');
  await forAwaitEach(asyncData, (item, index) => {
    console.log(`Async Item ${index}: ${item}`);
  });
  console.log('Finished asynchronous iteration.');
}

processAsyncIterable(asyncGenerator());

view raw JSON →