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

wentto

v0.0.4

Published

GOTO in Javascripts. Considered harmful.

Downloads

8

Readme

wentto Build Status

GOTO in Javascripts. Considered harmful.

Getting Started

Install the module with: npm install wentto

var wentto = require('wentto');

var doSomethingComplicated = wentto([
    function preCheck(go) {
        if (something) { return go('fail') }
        else if (somethingElse) { return go('fail') }
    },
    function doTheThing(go) {
        // Do the thing...
    },
    ['fail', function fail(go) {
        // Handle errors here
    }]
])

Documentation

require('wentto')([func0, func1, func2, func3, ...])

Pass it an array of functions. Make sure all of them accept the function "go" as a parameter.

wentto returns a function. That function will execute func0, then func1, etc. If one of these functions returns go(<index>), execution continues at func<index>.

return go(index)

Go to a function. The index argument is the index of the function in the array you passed to wentto(). So return go(0) to go back to the first function, return go(2) to go to the second function.

return go(label)

Go to a function which has a name. See the example below, "naming your goto labels", to see how to give your functions a name.

return go(indexOrLabel, args...)

Calls the next function (see the 2 above entries) with the arguments you passed.

Examples

achieving a loop

var a = 0;
var doSomethingUntil = wentto([
    function doSomething(go) {
        a += 1;
    },
    function until(go) {
        if (a < 3) {
            return go(0);
        }
    }
])

doSomethingUntil();

a // -> 3

accepting parameters

just add them to every function after the "go" param

var justSum = wentto([
    function (go, a, b) {
        return a + b;
    },
])

justSum(1, 2) // -> 3

returning early

Just return anything which is not undefined or go(somewhere)

var dontGoFar = wentto([
    function (go) { return 42 },
    function neverExecuted(go) {}
])

naming your goto labels

It can become complicated to deal with numeric indices in a large wentto chain.

To give names to your functions, just wrap them in a pair ([name, yourFunction]). You don't have to give a name to every function.

var myFunc = wentto([
    function (go) { if (something) return go('fail') },
    ['fail', function (go) { throw something }]
])

How does it work?

It's just a for loop which executes the input functions one by one. go is actually a class which wraps the index of the function you want to go to. If one of the functions returns an instance of go, the loop variable is changed (i = newIndex + 1; continue;).

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 0.0.4 Passing arguments to the next function
  • 0.0.3 Documentation fix
  • 0.0.2 Adds the possibility of naming each function in the wentto() chain.
  • 0.0.1 Implemented core functionality: passing an array of functions and using go() to goto other functions.

License

Copyright (c) 2014 Fábio Santos. Licensed under the MIT license.