![minified-size](https://img.shields.io/bundlephobia/min/iteragain)
Another Javascript library for iterating. Available on npm.js and jsr.io.
Pure JavaScript, Iterable/Iterator/Generator-function utilities. No dependencies and shipped with types as is.
Iterators and Iterables in Javascript has basically no supporting methods or functions. This package provides easy to use, lazy evaluation methods to save on memory and unnecessary processing. Support with using the already existing next()
method and usage in "for of" loops, this makes using Iterators as easy as using an Array.
This package can be used like in Python with standalone functions like map, filter, etc. Or be used with method chaining by calling iter which returns an instance of the ExtendedIterator class. Either is supported for however you want to handle iterators.
Documentation
You can see the full list of modules and the documentation on everything here.
Changelog
View the full list of changes here.
Code examples
A performance benefit over using Array's higher-order methods
const result1 = someArray.map(iteratee).filter(predicate).reduce(reducer);
const result2 = iter(someArray).map(iteratee).filter(predicate).reduce(reducer).toArray();
equal(result1, result2);
Basic use of iter
and some standalone functions
import { iter } from 'iteragain';
import iter from 'iteragain/iter';
let nums = iter([1, 2, 3, 4, 5])
.map(n => n * n)
.filter(n => n % 2 === 0)
.toArray();
import { map, filter, toArray } from 'iteragain';
nums = toArray(
filter(
map([1, 2, 3, 4, 5], n => n * n),
n => n % 2 === 0,
),
);
Using the range
standalone function
import range from 'iteragain/range';
range(10).toArray();
range(10, 0).toArray();
range(0, -10).toArray();
range(0, 10, 2).toArray();
let r = range(3);
const nums = [...r, ...r];
r = range(0, 10, 2);
r.length;
r.includes(4);
r.nth(-1);
r.nth(1);
r.nth(10);
r.index(4);
Iterating through any object's properties recursively
import iter from 'iteragain/iter';
const obj = { a: 1, b: { c: 2, d: { e: 3 }, f: 4 } };
const keys = iter(obj)
.map(([key, _value, _parent]) => key)
.toArray();
Supports passing functions as input and using an optional sentinel value to stop iteration
import iter from 'iteragain/iter';
const it = iter(() => someFunction(1, 2, 3), null);
const results = it.toArray();
Accessing elements from an iterator with arrayLike
The arrayLike
uses a Proxy that wraps a seekable iterator. This allows access to elements from the iterator. This can be useful when you may need to easily search behind the current element, then look ahead, or just access elements by index.
import { arrayLike, range, iter, toArray } from 'iteragain';
const arr = arrayLike(iter(range(100)).map(n => n * n));
arr.length;
arr[4];
arr.length;
arr[9];
arr.length;
arr[-1];
arr.length;
for (const n of arr) {
console.log(n);
if (n > 20) break;
}
const collected = toArray(arr);
Inpired by
iterplus, iterare, lodash, rxjs, ixjs and the Python itertools and more-itertools modules. See benchmark section for performance against some of these.
Benchmark
Starting benchmark suite: index.bm.ts
for of loop x 2,831 ops/sec, ±10 ops/sec or ±0.35% (98 runs sampled)
iteragain x 1,853 ops/sec, ±7 ops/sec or ±0.36% (98 runs sampled)
iteragain standalones x 1,740 ops/sec, ±27 ops/sec or ±1.57% (97 runs sampled)
iterare x 1,754 ops/sec, ±31 ops/sec or ±1.76% (96 runs sampled)
rxjs x 1,319 ops/sec, ±7 ops/sec or ±0.50% (96 runs sampled)
ixjs x 873 ops/sec, ±15 ops/sec or ±1.70% (95 runs sampled)