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

prat

v1.1.0

Published

easy peasy promise-aware transform streams

Downloads

175

Readme

prat

Build Status

Easy peasy promise-aware transform streams.

Example

First we'll write a function that returns a promise for a cities local weather:

var questor = require('questor');

var baseUri ='http://api.openweathermap.org/data/2.5/weather?units=metric&q=';

function getWeather (city) {
  var uri = baseUri + encodeURIComponent(city);
  return questor(uri).get('body').then(JSON.parse);
}

Then we'll pipe some data through it:

var prat = require('prat')
var from = require('from2')

var cities = [
  'Berlin,de',
  'Victoria,ca',
  'Las Palmas de Gran Canaria',
  'Tokyo',
  'Paris',
  'Calgary',
  'Phoenix',
  'Johannesburg'
]

var weather = from(cities).pipe(prat(getWeather));

By default each item will be processed serially, you can also control maximum number of concurrent operations by passing an options object as the first argument to prat:

var weather = from(cities).pipe(prat({concurrency: 5}, getWeather));

prat maintains the ordering of it's inputs even when processing multiple operations concurrently. It will also work just fine with synchronous functions:

var concat = require('concat-stream');
var namesAndDescriptions = weather.pipe(prat(function (w) {
  return [w.name, w.main && w.main.temp, w.weather[0].description];
}));

Finally, we'll log out our results:

namesAndDescriptions.on('data', console.log);

Convenience methods

Transform streams created by prat have some convenience methods:

  • prat.ify(stream) is equivalent to stream.pipe(prat(identity)).
  • map([opts,] fn) is equivalent to pipe(prat(opts, fn)).
  • filter([opts,] fn) given a predicate function (which may return a promise), return a stream containing only values for which the predicate function returned a truthy value
  • reduce(memo, fn) takes an initial value and returns a promise for the result of repeatedly calling memo = fn(memo, chunk) for each chunk in the stream. The reducer function can return a promise.
  • tap is the same as map, but only inspects objects instead of replacing them.

Using the above we could rewrite our weather example like this:

var ws = prat.ify(from(cities)).map(getWeather).reduce({}, function (report, w) {
    report[w.name] = w.main && Math.round(w.main.temp) + '° C, ' + w.weather[0].description
    return report;
  })
  .then(console.log);

Errors

Thrown errors and promise rejections will be emitted as normal 'error' events from the stream. As with all node streams, 'error' events are not forwarded when you pipe to another stream, and unhandled 'error' events will crash your process.

License

MIT