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

any-iter-utils

v1.0.5

Published

API for working with Iterable and AsyncIterable objects and some useful container types

Downloads

89

Readme

Utils for any iterable objects

build unit tests

Helpers for working with any iterable objects and some useful container types

Highly inspired by python itertools, rust std::iter and tc39 iterator-helpers

Getting started

Before start using the library use can additionally include prelude file which currenly contains only global types:

import 'any-iter-utils/prelude';

These types are used by the library and can be helpful if you decide to write your own helpers or something

See prelude.d.ts

If you want to use imports as it's shown below you should have typescript version >= 4.7 and moduleResolution setting in tsconfig.json:

{
  "compilerOptions": {
    "moduleResolution": "node16" // node16 | nodenext
  }
}

Otherwise you will have to import everything just from any-iter-utils not being able to specify the path. It will look like this:

import { map, SyncIter } from 'any-iter-utils';

Instead of this:

import { map } from 'any-iter-utils/combinators';
import { SyncIter } from 'any-iter-utils/containers';

It's because the library uses exports field in package.json which is not supported with other settings of typescript

Combinators

The basic usage of any helper would look like this:

import { map } from 'any-iter-utils/combinators';

const
  iterable = [1, 2, 3],
  mapper = (v: number) => v * 2;

map(iterable, mapper); // IterableIterator<2, 4, 6>

const asyncIterable = (async function* () {
  yield 1; yield 2; yield 3;
})();

map(asyncIterable, mapper); // AsyncIterableIterator<2, 4, 6>

See combinators for all available helpers

Collectors

If you want to collect your iterable into some data structure you can use collectors:

import { intoArr, intoSet } from 'any-iter-utils/collectors';

function* gen(): Generator<number> {
  yield 1; yield 2; yield 1;
}

intoArr(gen()); // [1, 2, 1]

async function* agen(): AsyncGenerator<number> {
  yield 1; yield 2; yield 1
}

intoSet(agen()); // Promise<Set<1, 2>>

Or a universal function collect. The example above is equivalent to:

import { collect } from 'any-iter-utils/collectors';

collect(gen(), []); // [1, 2, 1]
collect(agen(), new Set()); // Promise<Set<1, 2>>

Containers

Using only combinators sometimes can be more accurate but often it leads to something like this:

map(filter(map(filter(iterable, f1), f2), f3), f4);

To get rid of this problem you can use SyncIter and AsyncIter containers. The example above is equivalent to:

import { SyncIter } from 'any-iter-utils/containers';

SyncIter.from(iterable)
  .filter(f1)
  .map(f2)
  .filter(f3)
  .map(f4)

See containers for all available container types

Building

Install dependencies with npm ci, then run npm run build

The build command will generate esm bundle lib/esm, cjs bundle lib/cjs and declaration files in types folder