Skip to main content

A library to use and manipulate sync and async iterables like you would Arrays

This package works with Node.js, DenoIt is unknown whether this package works with Cloudflare Workers, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
100%
Published
3 months ago (2.5.4)

Polyethylene

Polyethylene is a wrapping layer around iterators and async iterators that lets you chain functional operators in a similar way you do with arrays but without the memory overhead or having to wait for an asynchronous iteration to end.

Version Tests

Basic Usage

The default export of polyethylene (named Poly throughout the documentation) is the main entry point. You would typically create an "augmented" iterable object using Poly.asyncFrom or Poly.syncFrom, then you start calling transform methods like .map, .filter, etc. in the returned object, ending with a leaf method like .reduce or .forEach.

In this way, polyethylene objects behave very similarly to Arrays, but they are fundamentally different because they don't store their elements anywhere, instead processing them one by one.

The following is a very simple, fictitious example of using polyethylene:

import Poly from 'polyethylene';
import {findUsers, findUserPosts} from 'some-api-lib'

// Print the first 10 posts of each user
await Poly.asyncFrom(findUsers())
  .flatMap(user => Poly.asyncFrom(findUserPosts(user)).take(10))
  .forEach(post => console.log(post));

CommonJS

This package is designed as an ECMAScript Module from the get go, but since 2.1.0 a CommonJS version is provided.

All named exports are supported, but the default export must be accessed via the default or Poly exports:

const Poly = require('polyethylene').default
const {Poly} = require('polyethylene')

Full Documentation

See the API Documentation.

License

Polyethylene is released under the MIT License

Add Package

deno add jsr:@dani/polyethylene

Import symbol

import * as polyethylene from "@dani/polyethylene";

---- OR ----

Import directly with a jsr specifier

import * as polyethylene from "jsr:@dani/polyethylene";

Add Package

npx jsr add @dani/polyethylene

Import symbol

import * as polyethylene from "@dani/polyethylene";

Add Package

yarn dlx jsr add @dani/polyethylene

Import symbol

import * as polyethylene from "@dani/polyethylene";

Add Package

pnpm dlx jsr add @dani/polyethylene

Import symbol

import * as polyethylene from "@dani/polyethylene";

Add Package

bunx jsr add @dani/polyethylene

Import symbol

import * as polyethylene from "@dani/polyethylene";