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

scoop

v0.0.2

Published

Just another control-flow library which is a little bit different.

Downloads

470

Readme

scoop

Another control-flow library for node with little style changes and tweaks.

License

MIT license

How to install

Just as every other node.js package, just install it via npm.

npm install scoop

Why anyother control-flow lib?

scoop looks and feels more or less like all the other control-flow libraries, some facts:

  • errors are catched and forwarded to the finalizer to remove clutter
  • all functions share a common 'context' which is passed as the first argument
  • results from callbacks are pushed into the global 'context'
  • callbacks are generated and handled by the special 'this' variable
  • scoop calls may be arbitrary deep nested

I really liked step for it's simplicity but it was also not perfectly what I wanted.

  • I realized I've never handled errors in a step except throwing it.
  • Sometimes the need to pass arguments down the chain and a closure or arguments just created clutter.
  • The real reason for scoop: I needed it to be nestable

How to use

Simple sample

From 'examples/simple.js':

scoop(
    function readFile(ctx) {
        // read the file and store the result in ctx['data']
        fs.readFile('test.json', 'utf8', this('data'));
    },
    function toJSON(ctx) {
        // read ctx['data'], transform it and save the result into ctx['json']
        ctx['json'] = JSON.parse(ctx['data']);
    })( // please note this line
    function finalize(err, ctx) {
        // test if there was any error
        if (err) throw err;

        // show that ctx['json'] is populated with the expected result
        console.log(ctx['json']['test']);
    }
);

The sample should be easy enough to get the main idea behind scoop. Whenever you call 'this' a callback is generated which excepts the usual callback style ('function (err, result)'). The argument used on 'this' is used as the 'key' for the result in the 'context'.

The context may be changed and read everytime you want and is never touched by scoop except for result population. You may also save the result of the first scoop call to specify a finailzer later.

Multiple results for one key

From 'examples/multiple.js':

scoop(
    function readFile(ctx) {
        fs.readFile('test.json', 'utf8', this('data'));
        fs.readFile('test.json', 'utf8', this('data'));
    },
    function toJSON(ctx) {
        ctx['json'] = [];
        for (i in ctx['data']) {
            ctx['json'].push(JSON.parse(ctx['data'][i]));
        }
    })(
    function finalize(err, ctx) {
        if (err) throw err;

        console.log(ctx['json']);
    }
);

If a key is used multiple times the results are automatically stored in an array where the order is equal to the initial call order.

Force Array creation

From 'examples/forcearray.js':

scoop(
    function readFile(ctx) {
        fs.readFile('test.json', 'utf8', this(['data']));
    },
    function toJSON(ctx) {
        ctx['json'] = JSON.parse(ctx['data'][0]);
    })(
    function finalize(err, ctx) {
        if (err) throw err;

        console.log(ctx['json']);
    }
);

You may also force the Array creation by passing the desired key within an array. This tells scoop to create an array as the result regardless of the number of results.

Nested calls

From 'examples/nested.js':

function readFiles(files, callback) {
    scoop(
        function read(ctx) {
            for (i in files) {
                fs.readFile(files[i], 'utf8', this(['data']));
            }
        })(
        function finalize(err, ctx) {
            if (err) {
                callback(err);
            } else {
                callback(null, ctx['data']);
            }
        }
    );
}

scoop(
    function readFile(ctx) {
        fs.readFile('test.json', 'utf8', this('data'));
        readFiles(['simple.js', 'multiple.js', 'forcearray.js'], this('data'));
    },
    function toJSON(ctx) {
        ctx['json'] = JSON.parse(ctx['data'][0]);
    })(
    function finalize(err, ctx) {
        if (err) throw err;

        console.log(ctx['data'][0]);
        console.log(ctx['data'][1].length);
    }
);

This example is combination of all the other samples with the addition of nesting. Please note that you aren't forced to use only one key for all the results you are expecting. Just use as many as you need.