npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zip-iterables

v2.0.0

Published

Convert an array of iterables to an iterable of arrays

Downloads

7

Readme

zip-iterables

This package provides:

  • A zip function that converts an array of Iterables into an Iterable of arrays.
  • zipAsync and zipAsyncParallel functions that do the same for AsyncIterables.
  • TypeScript overloads for n-tuples, such as <A, B>(Iterable<A>, Iterable<B>) => Iterable<[A, B]>.
  • Some utilities such as:
    • asyncIterableToArray()
    • iterableToAsyncIterable()
    • isAsyncIterable() and isIterable()
    • iteratorReturn(), iteratorThrow(), asyncIteratorReturn() and asyncIteratorThrow()

Example

import { zip } from "zip-iterables";

Array.from(zip(["a", "b", "c"], ["d", "e"], [1, 2]));

// Result:
// [
//   ['a', 'd', 1],
//   ['b', 'e', 2]
// ]

Why this package?

It is fairly trivial to implement a basic zip function:

// Example only. Do not do this in production.
function* zip<T>(...iterables: Iterable<T>[]): Iterable<T> {
  const iters = iterables.map(x => x[Symbol.iterator]());
  while (true) {
    const ret = new Array<T>();
    for (const iter of iters) {
      const { done, value } = iter.next();
      if (done) {
        return;
      }
      ret.push(value);
    }
    yield ret;
  }
}

However, the functions provided by this package have a number of important advantages over a naive implementation:

  1. The resulting Iterable or AsyncIterable can be looped over multiple times (i.e. it returns a fresh iterator in response to [Symbol.iterator]() and [Symbol.asyncIterator]()). This would not be the case if zip itself is a generator function (as above).
  2. All three of the next, return and throw methods, instead of just next, are forwarded to the inner iterators and the results collated.
  3. Return values (value of the IteratorResult when done = true) are also collated from inner iterators in addition to yielded values, with undefined used as the value for iterators that have not finished.
  4. As soon as next, return or throw on one of the iterators either throws or returns done = true, the other iterators are closed by calling the return method, if defined. This gives the iterator an opportunity to free any resources so they do not leak. For example, if the iterator is a generator, this executes any finally {..} blocks around the current yield.
  5. If any of the iterators throw in response to return, this does not prevent return from being called on the other iterators.
  6. If any of the iterators throw and an error has already been caught, the original error is re-thrown instead of the new one. (This is what TypeScript and Babel do for a for...of loop.)
  7. The return method is only called on iterators that have started (next called at least once) but not finished (next has not returned done = true) and not thrown. (This is what TypeScript and Babel do for a for...of loop.)
  8. Any input passed to the zipped iterator via the next, return and throw methods is forwarded to the individual iterators.
  9. For zipAsyncParallel, the next, return and throw methods of the AsyncIterators are executed in parallel to maximise throughput.