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

use-goto

v1.0.0

Published

allow to use the power of goto in JavaScript!

Downloads

10

Readme

use-goto

allows to use the power of goto in JavaScript!

Once again inspired by Tsoding implementing goto in OCaml, I decided to try to do the same in JavaScript.

It's worth to remember that JavaScript already has GOTO-like functionality in the form of break and continue as I described in https://kentaromiura.wordpress.com/2014/05/25/hoppa-goto-style/, but I think this use-* utility, could actually be useful when handling errors.

In the OCaml implementation above Exception has been abused for forcing code jumping around, I realized it's not needed if adding return in front of goto as per example:

var {goto, start} = useGoto({
    loop(){
        if (i>=10) return goto('out');
        console.log(`hello world, ${i}`);
        i++;
        return goto('loop');
    },
    out() {
        console.log('done.')
    }
});
start();

As you can see from the example above it's possible to use object method definitions

instead of having to use strings for labels, this way it looks just a bit better IMHO.

The code is written in ES3 compliant mode (with the exception of Object.keys, of which you'll have to include the polyfill) so you can use this in your IE6 adventures.

Various notes:

  • if your flow will continue to the next block you can omit return, eg: in the example above return goto('loop'); can be safely replaced with just goto('loop')
  • if your flow will continue to the next flow you can omit the goto from a return, in the example above if (i>=10) return goto('out'); the goto('out') can be left out.
  • start is synchronous so you don't need to await for it and can safely return the value you've collected, like in the repository test.js
  • this rely on the fact that JavaScript preserve the definition of properties in order of the source, this is not always true, especially in case you define properties that can be numbers, in that case this will break, just don't use Symbols or numbers for labels and you're ok.