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

@k88/switch

v1.2.0

Published

<h1 align="center">@k88/switch</h1> <p align="center">A declarative and functional replacement of JavaScript `switch` statement</p>

Downloads

1

Readme

Installation

Install using npm by running

npm install @k88/switch

Usage

Switch creates a declarative chains of when to build up your case statements:

import Switch from '@k88/switch';

const response = Switch(...variables)
    .when(Predictor, Matched)
    .otherwise(Matched)

It can take a variable number of arguments.

The when clause takes two arguments, Predictor and Matched response; Predictor is a callback method that returns boolean whether there is a match. If there is a match, the Matched callback is invoked.

The otherwise cause takes a Matched clause and is invoked if none of the Predictors from the when clause are matched.

Examples

Example with single argument

import Switch from '@k88/switch';

const response = Switch(variable)
    .when(x => x < 5, () => 'ok')
    .when(x => x < 10, () => 'warning')
    .when(x => x < 100, () => 'error')
    .otherwise(() => 'critical');

// For `variable = 1`, you get `ok`, matching the 1st when clause
// For `variable = 10`, you get `error`, marching the 3rd clause
// For `variable = 1000`, you get `critical`, because none of the when clauses match, so otherwise clause is used

Example with multiple arguments

import Switch from '@k88/switch';

const response = Switch(variable1, variable2)
    .when((x, y) => x > y, () => x)
    .when((x, y) => x < y, () => y)
    .otherwise(() => x);

// This is a simple comparator that effectively performs the same task as
const max = Math.max(variable1, variable2);

Predictor

The Predictor is a callback function that receives the number of variables we are testing against, and expects a boolean response.

const predictor = (arg1, arg2, arg3, ...) => {
  // return true/false
}

// use this predictor in the `.when` method

Helper Predictors

There are a few common helpers that SwithCase provides

is Predictor
import Switch, { is } from '@k88/switch';

const resp = Switch(error)
    .when(is(CustomError), () => 'this is a custom-error')
    .when(is(Error), () => 'this is an error')
    .otherwise(() => 'This is unknown');

This predictor checks that the variable passed is an instanceof CustomError or Error.

eq Predictor
import Switch, { eq } from '@k88/switch';

const resp = Switch(variable)
    .when(eq(5), () => 'equals 5')
    .otherwise(() => 'does not equal 5');

The predictor does a === check.

Creating Custom Predictors

A custom predictor is just a function that takes in some argument, and returns a predictor. For example the eq predictor is

function eq(...values) {
    // The return function is the predictor
	return (...variables) => {
		return variables
			.every((variable, index) => variable === values[index]);
	}
}

Matched

The Matched callback is invoked when the when clause of the predictor is truthy. This method takes no argument and should return the result.

If your result is a simple value back, you can use the doReturn helper callback:

import Switch, { eq, doReturn } from  '@k88/switch';

const resp = Switch(variable)
    .when(eq(5), doReturn('this is 5'))
    .otherwise(doReturn('this is 5'))