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

trnsd

v1.2.4

Published

Map and filter that handle async functions, sequentially or in parallel.

Downloads

96

Readme

trnsd

naivistic but async-enabled transducer (micro-)library

The transducer approach can be used to improve the 'ordinary' array map an filter operations that we know and love.

  1. by not having to create a new collection for each map/filter operation (this can improve efficiency with large collections, and is the motivating example in various blog posts, as well as [1])
  2. making it more convenient to use async functions in map and filter. This means that you don't need to wrap your map result in Promise.all. The goal in this case is to improve readability rather than speed (Two approaches are possible; either to wait for every promise, so items will be processed sequentially. The other is to start processing every input alement at once.)

Transducer constructors

There are two constructors map and filter nothing special here

Basic interface

The basic interface is a function tr_array which expects an array (or another type of iterable object) of input values followed by any number of maps or filters. It traverses (hence the tr_ prefix) the input collection, passes each element through the provided stages, and returns a new array with the results.

 const { tr_array, map, filter } = require('trnsd')
, numbers = [1, 2, 4, 5, 6, 7, 8, 9, 10]

const result = tr_array(
  numbers,
  map(x => x * 50),
  map(x => x + 1),
  filter(x => x < 400),
  filter(x => Math.floor(x/100) % 2 === 0)
)

// [ 51, 201, 251 ]

Async interface

There are two variants; both return a Promise that resolves to a new array with the results.

In sequence

const { tr_async, map, filter } = require('trnsd')
const { wait } = require('./wait')
, numbers = [1, 2, 4, 5, 6, 7, 8, 9, 10]

tr_async(
  numbers,
  map(x => x * 50),
  map(x => Promise.resolve(x + 1)),
  filter(x => wait(400).then(() => x < 400)),
  filter(x => Math.floor(x/100) % 2 === 0)
)
.then(console.log) // [ 51, 201, 251 ]

In parallel

use tr_par instead of tr_async. Parallel can be nice for speed, but error handling can be tricky: Similar to using Promise.all, your .catch handler is called at most once — if errors happen after the first one, they are ignored!

Note that

  • Functions passed to map and filter may return a value or a Promise... But:
  • if Promises are involved, use tr_async or trnsd_async
  • tr_async and tr_par always return a Promise
  • tr_array dies in flames if a Promise is encountered. (UnhandledPromiseRejectionWarning.)

trnsd, trnsd_async and trnsd_par interface

these are proper "transduce" type functions, see examples/example_custom.js

References

[1] Egghead transducer tutorial (https://egghead.io/courses/quickly-transform-data-with-transducers)