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

ay

v0.1.1

Published

Chainable array operations suitable for use inside coroutine generators (NB. this is NOT about lazy list transformation!)

Downloads

20

Readme

ay

Chainable array operations suitable for use inside coroutine generators (NB. this is NOT about lazy list transformation!)

The a is for array, the y is for yield.

Installation

NPM

In a generator being driven by something like co such as a koa route or middleware, you may want to work on an array with the standard ES Array methods: forEach, map, reduce, every, some, reduce, reduceRight.

This is fine except that you may also want to call one or more asynchronous helpers or APIs from within the callback you pass to the array method. For maximum clarity you'd like to use yield in the usual way. But the array methods don't accept a generator - only an ordinary function.

Example:

app.get('/users', function* () {
    var userIds = yield data.getUserIds();
    this.body = userIds.map(function(userId) {
        // THIS DOESN'T WORK
        return data.getUserInfo(userId);
    });
});

The call to data.getUserInfo needs to return an object describing a user. But it's asynchronous, so it actually returns a thunk, a promise, a generator, etc.

Using ay, we can wrap an ordinary array in an object that has the same callback-taking methods, but they take generators instead of ordinary functions:

var ay = require('ay');

app.get('/users', function* () {
    var userIds = yield data.getUserIds();
    this.body = yield ay(userIds).map(function* (userId) {
        return yield data.getUserInfo(userId);  
    });
});

The map function returns another ay wrapper, so further calls can be chained. An ay wrapper can be yielded to get the resulting array (it is "thenable", so mimics a promise sufficiently for co). This has the effect of "transmitting" the yields inside the callback to the outer context so they can be managed by the coroutine runner.

An chaining example, assuming an async sleep function:

var num = yield ay(nums)
    .map(function* (i) {
        yield sleep(10);
        return i * 2;
    })
    .filter(function* (i) {
        yield sleep(10);
        return i > 2;
    })
    .reduce(function* (a, b) {
        yield sleep(10);
        return a + b;
    });

Due to the high precedence of yield, it applies to the entire ay-expression and so the result of reduce is yielded.

Note that only the standard array methods that accept a callback are included. For other methods you can easily yield to get an ordinary array - note the placement of the parentheses:

var moreNums = (yield ay(nums)
    .map(function* (i) {
        yield sleep(10);
        return i * 2;
    }))
    .concat([10, 20, 30]);

A real example using `forEach, from bitstupid.com:

app.get('/bits/:of', function* () {
    var bit = yield data.readBit(
        this.params.of.toLowerCase(), 
        this.query.skip, 
        this.query.take);

    yield ay(bit.changes).forEach(function* (change) {
        change.info = yield data.getInfo(change.by);
    });

    this.body = bit;
});