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

either-or

v0.0.2

Published

Write code as branches to be enumerated

Downloads

1

Readme

either-or

TL;DR

Write code as branches to be enumerated.

var EitherOr = require('either-or');

EitherOr.runAll(function(eo) {
  console.log("Hello");
  eo.either(function() {
    console.log("Doctor Jekill");
  }).or(function() {
    console.log("Mister Hide");
  });
  console.log("Have a nice day");
});

Prints:

Hello
Doctor Jekill
Have a nice day
Hello
Mister Hide
Have a nice day

Install

Install the package:

npm install either-or

Use it:

var EitherOr = require('either-or');

Use

Either... or...

Multiple branches

Try two branches:

EitherOr.runAll(function(eo) {
  console.log("Hello");
  eo.either(function() {
    console.log("Doctor Jekill");
  }).or(function() {
    console.log("Mister Hide");
  });
  console.log("Have a nice day");
});

Prints:

Hello
Doctor Jekill
Have a nice day
Hello
Mister Hide
Have a nice day

You can add as many or as needed:

EitherOr.runAll(function(eo) {
  console.log("Hello");
  eo.either(function() {
    console.log("Doctor Jekill");
  }).or(function() {
    console.log("Mister Hide");
  }).or(function() {
    console.log("Mister Utterson");
  });
  console.log("Have a nice day");
});

Conditions

You can use conditions to skip branches:

var happyEnding = true;
EitherOr.runAll(function(eo) {
  console.log("Hello");
  eo.either(function() {
    console.log("Doctor Jekill");
  }).or({if: !happyEnding}, function() {
    console.log("Mister Hide");
  });
  console.log("Have a nice day");
});

Prints:

Hello
Doctor Jekill
Have a nice day

Choose

Enumerate arrays

You can also enumerate the elements of an array:

EitherOr.runAll(function(eo) {
  console.log("Hello");
  var name = eo.choose(["Doctor Jekill", "Mister Hide"]);
  console.log(name);
  console.log("Have a nice day");
});

Prints:

Hello
Doctor Jekill
Have a nice day
Hello
Mister Hide
Have a nice day

Conditions

You can filter the elements:

EitherOr.runAll(function(eo) {
  console.log("Hello");
  var name = eo.choose(["Doctor Jekill", "Mister Hide"], {
    with: function(e) { return e.startsWith("Doctor"); }
  });
  console.log(name);
  console.log("Have a nice day");
});

Prints:

Hello
Doctor Jekill
Have a nice day

Combine

You can also combine these constructs at will.

Nest either/or:

EitherOr.runAll(function(eo) {
  console.log("Hello");
  eo.either(function() {
    console.log("Doctor Jekill");
  }).or(function() {
    console.log("Mister Hide");
    eo.either(function() {
      console.log("O mysterious character");
    }).or(function() {
      console.log("O dangerous man");
    });
  });
  console.log("Have a nice day");
  console.log("");
});

Prints:

Hello
Doctor Jekill
Have a nice day

Hello
Mister Hide
O mysterious character
Have a nice day

Hello
Mister Hide
O dangerous man
Have a nice day

Either/or in series:

EitherOr.runAll(function(eo) {
  console.log("Hello");

  eo.either(function() {
    console.log("Doctor Jekill");
  }).or(function() {
    console.log("Mister Hide");
  });

  eo.either(function() {
    console.log("Have a nice day");
  }).or(function() {
    console.log("I wish we never met");
  });

  console.log("");
});

Prints:

Hello
Doctor Jekill
Have a nice day

Hello
Doctor Jekill
I wish we never met

Hello
Mister Hide
Have a nice day

Hello
Mister Hide
I wish we never met

Either/or and choose:

EitherOr.runAll(function(eo) {
  console.log("Hello");
  var name = eo.choose(["Doctor Jekill", "Mister Hide"]);

  eo.either(function() {
    console.log(name);
  }).or(function() {
    console.log(name.toUpperCase());
  });
  console.log("Have a nice day");
  console.log("");
});

Prints:

Hello
Doctor Jekill
Have a nice day

Hello
DOCTOR JEKILL
Have a nice day

Hello
Mister Hide
Have a nice day

Hello
MISTER HIDE
Have a nice day

Conditions can help combine more complex data:

EitherOr.runAll(function(eo) {
  console.log("Hello");

  var name = eo.choose(["Doctor Jekill", "Mister Hide"]);

  eo.either(function() {
    console.log("Nice to meet you");
  }).or({if: (name.starsWith('Doctor'))}, function() {
    console.log("Could you examine my cough?");
  });

  console.log("Have a nice day");
  console.log("");
});

Prints:

Hello
Doctor Jekill
Nice to meet you
Have a nice day

Hello
Doctor Jekill
Could you examine my cough?
Have a nice day

Hello
Mister Hide
Nice to meet you
Have a nice day

Contribute

Pull requests are very welcome. If you add or change some code, by all means, write some tests.

If you feel your PR is quite obvious (typo, bug...), you can submit it directly. If it is a new feature or a change it the package behavior, first post an issue to discuss it.