Repository navigation
v2.0.0
A rewrite of @hyperjump/pact. The documentation is now at https://hyperjump.io/pact/.
What's new
- Faster. The iterators are hand-written classes instead of generators. Sync pipelines are 2 to 15 times faster than in v1, and async pipelines are about 1.5 times faster. See Benchmarks for how pipelines compare with array methods.
compose. It builds a pipeline without a source, which you can use with any number of iterables or put in another pipeline. See Pipelines.- New functions.
flatMap,forEach, andzipKeyed.rangetakes a step, andziptakes any number of iterables and hasshortest,longest, andstrictmodes. - Native helper methods. Every iterator pact returns is an
Iterator, so the native helper methods work on it. See Native helpers. - Async iterators read in order and can be disposed. Overlapping
next()calls are queued, and every async iterator has[Symbol.asyncDispose](). See Async.
Breaking changes
- Every function is a step. It takes only its configuration and returns a function that takes the iterable:
map(fn, iterable)ismap(fn)(iterable), or a step in apipe.flattentakes only the depth. - The root module has only the sync functions. The async ones are in
@hyperjump/pact/async, without theasyncprefix:asyncMapismapfrom there. The types lost the prefix too. collectArray,collectSet,collectMap, andcollectObjectaretoArray,toSet,toMap, andtoObject.ziptakes an array of iterables instead of two arguments, and stops when any of them is done. Use{ mode: "longest" }to keep going.- Callbacks get each value's index as the argument after the value, so
map(parseInt)is wrong. Usemap((s) => parseInt(s)). reducewithout an initial value starts with the first value, and throws aTypeErrorfor an empty iterable.headcloses the iterator it reads from.concatthrows aTypeErrorfor anything that isn't an iterable object, including a string.- A step gets an iterator from its iterable when it's called with it, so something that isn't iterable throws right away.
- The functions return an
IteratorObjectinstead of aGenerator, without athrow()method. - It needs Node.js 24 or later, or Chrome 122, Firefox 131, or Safari 18.4 or later. Its types need TypeScript 5.6 or later.
See Migrating from v1 for each change and what to do about it.