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

itch

v0.2.2

Published

A flexible and readable switch statement replacement library for JavaScript

Downloads

7

Readme

itch

Flexible and readable switch statement replacement library for JavaScript

Basic Usage

var itch = require('itch');

var price = itch(item)
   .match('Orange').then(0.59)
   .match('Apple').then(0.32)
   .match('Banana').then(0.48)
   .match('Cherry').then(3.00)
   .matchOneOf(['Mango', 'Papaya']).then(2.79)
   .scratch(999.99)

This code is functionally equivalent to the following alternative standard implementations:

Switch

var price = (function() {
   switch(item) {
      case 'Orange':
         return 0.59;
      case 'Apple':
         return 0.32;
      case 'Banana':
         return 0.48;
      case 'Cherry':
         return 3.00;
      case 'Mango':
      case 'Papaya':
         return 2.79;
      default:
         return 999.99;
   }
})();

If-else

var price = (function() {
   if (item === 'Orange') {
      return 0.59;
   } else if (item === 'Apple') {
      return 0.32;
   } else if (item === 'Banana') {
      return 0.48;
   } else if (item === 'Cherry') {
      return 3.00;
   } else if (item === 'Mango' || item === 'Papaya') {
      return 2.79;
   } else {
      return 999.99;
   }
})();

Object Literals

var price = {
   Orange: 0.59,
   Apple: 0.32,
   Banana: 0.48,
   Cherry: 3.00,
   Mango: 2.79,
   Papaya: 2.79
}[item] || 999.99;

Nested Ternaries

var price = (item === 'Orange')
   ? 0.59
   : (item === 'Apple')
      ? 0.32
      : (item === 'Banana')
         ? 0.48
         : (item === 'Cherry')
            ? 3.00
            : (item === 'Mango' || item === 'Papaya')
               ? 2.79
               : 999.99;

Syntax

itch(seed)
   .match(candidate).then(returnValue)
   .match(anotherCandidate).then(anotherReturnValue)
   // ...
   .scratch(defaultValue)
  • An itch statement is a function chain that starts with a call to the itch function with a seed value.
  • The itch() call is typically followed by a series of match().then() calls.
  • match() accepts one argument, which is a candidate value to test for equality against the seed value.
  • then() accepts one argument, which is the value that will be returned if the seed matches the current candidate.
  • evaluate() may be used instead of then(). If the argument to evaluate() is a function, it will be evaluated only if the seed matches the current candidate.
  • The chain must be concluded with a scratch() call, whose sole argument is the default return value, should none of the candidates match the seed.

Custom Matchers

Add a using() call in the chain before a match() to use a custom matching function thereafter:

itch(-1)
   .using(function(a, b) {
      return b === -a;
   })
   .match(1).then(true)
   .scratch(false);
// returns true

The following are equivalent:

itch(2)
   .using(function(a, b) {
      return b.some(function(bb) {
         return bb === a;
      });
   })
   .match([1, 2, 3]).then(true)
   .scratch(false);
// returns true
itch(2)
   .matchOneOf([1, 2, 3]).then(true)
   .scratch(false);
// returns true

Evaluate

Often you will want to use the return value of a function call rather than a simple value as an argument to a then() function. In these cases, you should wrap the function call in an anonymous function and pass it to execute() instead. If a function invocation is passed to a then() function it will execute even if the corresponding candidate does not match, whereas a function passed to then() will simply be returned without being evaluated. evaluate() allows itch to be used in a greater number of scenarios.

Similarly scratchEvaluate() can be used instead of scratch() in these cases.

scratchEvaluate() : scratch() :: evaluate() : then()

// ES2015
return itch(command)
   .match('log in').evaluate(() => http.post('login', credentials))
   .match('log out').evaluate(() => http.post('logout'))
   .scratchEvaluate(() => Promise.reject(new Error('Invalid command')));