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

expecto

v0.2.4

Published

expecto

Downloads

16

Readme

expecto

See patterns in streams of data, send other data back.

Why?

For the same reasons *nix expect utility exists. Control interactive applications, parse data that is hard to parse using traditional techniques, and more.

Usage

expecto reacts to data coming from an input stream and tries to match it against a set of patterns you give to it. When a pattern matches, it fulfills a promise it gave you. If pattern could not be matched — because of timeout, end of stream, or matching another pattern — the promise will be rejected.

var e = expecto();
e.expect(/hello/).then(function (match) {
    e.send('hi');
});

Sequential expectations

Promises are natural for expressing chains of actions. This is how you first wait for a, and then b, and then c:

var e = expecto();
e.expect(/a/)
    .then(function () {
        return e.expect(/b/);
    })
    .then(function () {
        return e.expect(/c/);
    });

Maybe this looks kinda strange, because .expect(/a/) is indented differently from .expect(/b/) and .expect(/c/). If you are OCD like that, try:

var e = expecto();
Promise.resolve()
    .then(function () {
        return e.expect(/a/)
    })
    .then(function () {
        return e.expect(/b/);
    })
    .then(function () {
        return e.expect(/c/);
    });

Concurrent expectations

Sometimes you expect to see more than one possible pattern to occur. For example, at some point in data one of /a/, /b/ or /c/ may match, and you want very different actions taken for each of them. This is how you handle it:

var e = expecto();
e.expect(/a/).then(function () { console.log('a won'); });
e.expect(/b/).then(function () { console.log('a and c lost'); });
e.expect(/c/).then(function () { console.log('i can c you'); });

Joining on concurrent expectations

Suppose we expect either /a/ or /b/, but what follows them should be /z/. You can use standard Promise.race to join on first two concurrent expectations, and then wait for third.

Promise.race([
    e.expect(/a/),
    e.expect(/b/)
]).finally(function () {
    return e.expect(/z/);
});

Because one of two expectations will be rejected, we use .finally here.

Loops

When you expect the same pattern over and over, until some other pattern occurs, you can use something like this:

function aaaaaaz() {
    return new Promise(function (fulfill, reject) {
        function loop() {
            e.expect(/a/)
                .then(doSomethingWithA)
                .then(loop);

            e.expect(/z/).then(fulfill);
        }

        loop();
    });
}

aaaaaaz().then(/* ... */);

Given the input aardvark zoo, the aaaaaaz() would call doSomethingWithA three times, and then "exit" the loop, leaving the remaining oo in the buffer.

"But I hate Promises and want my pyramide of doom back!"

Sigh. Alright. You can pass Node style callbacks to methods of Expecto, and they will be called with cb(err, match) parameters.

API

  • expecto(options)
  • expecto.Promise
  • expecto.spawn(command[, arguments[, options]])
  • Expecto.prototype
    • .expect(pattern)
    • .timeout(ms)

expecto(options)

expecto creates Expecto objects that read data from options.input ReadableStream and send replies into an options.output WritableStream. Default values for those are process.stdin and process.stdout. Those could be any kind of Node stream: TTY, file, HTTP, etc.

Example:

var input = process.stdin;
var output = fs.createWriteStream('out.txt');
var e = expecto({ input: input, output: output });

expecto.Promise

A constructor function that is used to create promises returned by .expect() and others.

expecto is built around awesome bluebird promises.

expecto.spawn(command[, arguments[, options]])

Spawn a child process expecting data from its stdout and replying to stdin.

Expecto.prototype.expect(pattern)

Returns an expectation — a Promise which will be fulfilled as soon as pattern is encountered in input.

The expectation will be rejected if:

  1. a concurrent expectation matches instead;
  2. expectation times out;
  3. an end of data is reached and pattern is not there.

Example:

e.expect(/something/).then(function (match) {
    console.log('`something` found');
}, function (err) {
    console.error('`something` is not found :(');
});

.expect() implicitly starts a default .timeout() timer if there isn't one already. Meeting an expectation will clear the timeout.

Expecto.prototype.timeout(ms)

Returns an expectation which will be fulfilled after ms milliseconds — but only if a none of concurrent expectations fulfill during this period. In latter case, the promise rejects.

Example:

e.expect(/foo/).then(function () {
    console.log('`foo` found');
});
e.timeout(1000).then(function () {
    console.error('a whole second passed, but there is still no `foo`');
});

LICENSE

The MIT License (MIT)

Copyright © 2014 Stepan Stolyarov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.