iteragain
Another Javascript library for iterating.
Pure JavaScript, Iterable/Iterator/Generator-function utilities. No dependencies and shipped with types as is.
The package is designed around the use of the ExtendedIterator class through calling iter. It's a class that implements and extends the IterableIterator interface. It provides extra methods like those in the JS Array and other iterator methods in Python. It's chainable and looks closely like normal Javascript code instead of the more Python itertools way of doing things:
filter(lambda x: x % 2 == 0, map(lambda x: x * x, iterable))
There is also other utility functions like range
, enumerate
, zip
etc.
See documentation.
Code demo
import { iter } from 'iteragain';
import iter from 'iteragain/iter';
const nums = iter([1, 2, 3])
.map(n => n * n)
.filter(n => n % 2 === 0)
.toArray();
import range from 'iteragain/range';
range(10).toArray();
range(10, 0).toArray();
range(0, -10).toArray();
range(0, 10, 2).toArray();
let r = range(0, 10, 2);
r.length;
r.includes(4);
r.nth(-1);
r.nth(1);
r.nth(10);
r.index(4);
r = range(3);
const nums = [...r, ...r];
Inpired by
iterplus, iterare, lodash, rxjs and the Python itertools module. See benchmark section for performance against some of these.
Benchmark
Starting benchmark suite: index.bm.ts
for of loop x 2,015 ops/sec, ±47 ops/sec or ±2.33% (90 runs sampled)
iteragain x 1,431 ops/sec, ±25 ops/sec or ±1.74% (92 runs sampled)
iterare x 1,368 ops/sec, ±20 ops/sec or ±1.48% (95 runs sampled)
rxjs x 989 ops/sec, ±10 ops/sec or ±1.05% (93 runs sampled)