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

dolla-switch

v2.0.2

Published

Switch statements to be used as expressions, for vegetarians, or otherwise

Downloads

4,147

Readme

$switch 💰Expression based switch branching.

You can read this as dolla'-switch.

Javascript's switch is unfortunately not an expression, which means you can't use it as a right hand side of an expression or in places where you can only put a single value. Luckily, $switch can function as an expression.

Why would I use this over switch?

Well, the whole point of $switch is that it's an expression, that means, you can use it wherever you'd use a value: in-line evaluations, return statements, if statements, heck, you can use it in the key-expression for an object (but that's just ugly).

You can do neat expressions like this:

const constructor = $switch(typeof x, 
  { 'number': () => Number,
    'string': () => String,
    'date':   () => Date,
    'object': () => Object },
    () => null);

And the best part, it's Typescript-powered, that means, you'll get completion assistance and typecheck for exhaustiveness (when using $switch.strict).

Examples

Take this basic example, that demonstrates the usage as an expression:

type Day = 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'phriday' | 'saturday' | 'sunday';
const today: Day = pickRandom('monday', 'phriday', 'sunday') as Day;

// Inline, you have no option but to define a 'let' / 'var', and assign it in a switch:
let drinkOfTheDay = 'water or something';
switch (today) {
    case 'phriday':
        drinkOfTheDay = 'beer';
        break;
    case 'saturday':
        drinkOfTheDay = 'wine';
        break;
    case 'sunday':
        drinkOfTheDay = 'vodka';
        break;
    default:
        drinkOfTheDay = 'water';
        break;
}
// Or optionally, you can switch the above in a function, or an IIFE and return from each 'case'
// - but that doesn't make it any prettier. 


// With $switch:
import $switch from 'dolla-switch';

const drinkOfTheDay = $switch(day, {
        phriday: () => 'beer',
        saturday: () => 'wine',
        sunday: () => 'vodka',
    }, () => 'water');
}

Working with regular switch statements would be a pain in jsx - $switch is convenient here as well:

<div> Today is {today}!</div>
<div>
{
  $switch(today, {
    'phriday': () => <GifPartyParrot />,
    'saturday': () => <GifDancingDog />,
    'sunday': () => <GifSleepyGary />
  }, () => <GifWorkingPerson />)
}
</div>

With $switch.strict - you get even more safety, courtesy of Typescript:

const today: Day = 'phriday';

const drinkOfTheDay$ = $switch.strict(today, {
    phriday: () => 'beer',
    saturday: () => 'wine', // Errors for extra cases, because from the types it's visible, that 'today' can only be 'phriday' 
    sunday: () => 'vodka',
});

const today: Day = 'phriday' as Day;

const drinkOfTheDay$ = $switch.strict(today, { // Errors for the cases object, because
    phriday: () => 'beer',                     // the rest of the cases aren't handled
    saturday: () => 'wine',
    sunday: () => 'vodka',
});

For your cases, give it a spin.

Installation

For now, you can only use this with node.js, or using a bundler, because it's not exposing any globals, but who are we kidding, it's 2020, you probably do this daily already.

npm install --save dolla-switch

Examples

And $switch also supports types through Typescript!

Contains no meat™!