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

suicchi

v1.0.6

Published

Switch case on steroids

Downloads

15

Readme

suicchi

codecov Greenkeeper badge Build Status NPM Version Downloads Stats Donate

Better and cleaner switch case made for everyone

Installation

You can start by installing this library using the command below:

npm i --save suicchi

Run Test

npm run coverage

Basic Usage

import { Suicchi } from "suicchi";

const switchCase = new Suicchi();

switchCase.addCase("car", "Ford GT");
switchCase.addCase("name", "Rye");
switchCase.addCase("gender", "female");

const name = switchCase.evaluate("name");

console.log(name); // => "Rye"

// the above code will translate to:
// let name;

// switch ("name") {
//   case "car":
//     name = "Ford GT";¸¸
//     break;
//   case "name":˚v
//     name = "Rye";
//     break;
//   case "gender":
//     name = "female";
//     break;
//   default:
//     name = () => {}
//     break;
// }

// console.log(name);

Adding a default case

import { Suicchi } from "suicchi";

const defaultCase = "no-record";

const switchCase = new Suicchi(defaultCase);

switchCase.addCase("car", "Ford GT");
switchCase.addCase("name", "Rye");
switchCase.addCase("gender", "female");

const age = switchCase.evaluate("age");

console.log(age); // => "no-record"

// the above code will translate to
// let age;

// switch ("age") {
//   case "car":
//     age = "Ford GT";
//     break;
//   case "name":
//     age = "Rye";
//     break;
//   case "gender":
//     age = "female";
//     break;
//   default:
//     age = "no-record";
//     break;
// }

// console.log(age);

Multiple keys

import { Suicchi } from "suicchi";

const switchCase = new Suicchi();

switchCase.addCase(["car", "transportation"], "Ford GT");
switchCase.addCase("name", "Rye");
switchCase.addCase("gender", "female");

const car = switchCase.evaluate("car");

console.log(car); // will return "Ford GT"

// the above code will translate to
// let car;

// switch ("name") {
//   case "car":
//   case "transportation":
//     car = "Ford GT";
//     break;
//   case "name":
//     car = "Rye";
//     break;
//   case "gender":
//     car = "female";
//     break;
//   default:
//     car = () => {}
//     break;
// }

API

Constructor

The Suicchi Object constructor only takes in 1 optional parameter which can either a value, a function, or an object. And it generates the case depending on the type of parameter you pass in.

If you only pass in a value or a function, like the following:

const Switch = new Suicchi(() => {
  // DO OTHER THINGS...
  return 1 + 1;
});
const aSwitch = new Suicchi(() => ('aValue'));
const bSwitch = new Suicchi('anotherValue');

You'll be doing something equivalent to:

// Switch
switch(x) {
  default:
    // DO OTHER THINGS...
    return 1 + 1;
}

// aSwitch
switch(x) {
  default:
    return 'aValue';
}

// bSwitch
switch(x) {
  default:
    return 'anotherValue';
}

But if you pass in an Object, you'll be able to pass in other cases and routines besides the default case and routine, like so: (Note: if you pass in an object - the 'default' property will be required)

const cSwitch = new Suicchi({
  default: null;
  case1: "What";
  case2: () => (1234)
})

You'll be doing something similar to:

// cSwitch
switch(x) {
  case 'case1':
    return 'What';

  case 'case2':
    return 1234;

  default:
    return null;
}

AddCase

The AddCase method allows you to add new or overwrite existing cases. It takes in 2 parameters - the case and the routine.

The case can be of the following types: string, string[], or object;

if the case is an object, then the 2nd parameter, the routine, is no longer required as it should be paired into the key-value case object.

Example of use:

const x = new Suicchi();

// To assign a single case to a single routine
x.addCase("aValue", "anotherValue");

// the above is equivalent to:
switch(X) {
  case "aValue":
    return "anotherValue";

  // by default, this is already present at this point
  // as this is set upon initialization of the Suicchi object instance
  default:
    return null;
}

// To assign a multiple cases to a single routine
x.addCase(["val1", "val2"], "anotherValue");

// the above is equivalent to:
switch(X) {
  case "val1":
  case "val2":
    return "anotherValue";

  default:
    return null;
}

// To assign multiple cases to multiple routines
x.addCase({
  condition1: "12345",
  condition2: 12345,
});

// the above is equivalent to:
switch(X) {
  // ...EXISTING CASES AND ROUTINES
  case "condition1":
    return "12345";

  case "condition2":
    return 12345;

  // ...OTHER EXISTING CASES AND ROUTINES
  default:
    return null;
}

GetCases

The GetCases method will return a string array containing the existing cases that you've set for the Suicchi instance.

const x = new Suicchi({
  default: null;
  case1: "What";
  case2: () => (1234)
})

x.getCases(); // => ['case1', 'case2', 'default']

EvaluateCase (a.k.a Evaluate)

The EvaluateCase (or Evaluate, as it's still supported atm) method lets you run a specific case by passing in the case as a parameter.

if the provided parameter is does not match any of the cases, it will run the default routine.

Also, the case parameter is case-sensitive.

const x = new Suicchi({
  default: "Awesome",
  Rye: "Gay",
  rye: "Gay",
  rYe: "Gay",
  ryE: "Gay"
});

x.EvaluateCase("Ray");   // => "Awesome"
x.EvaluateCase("Jacob"); // => "Awesome"
x.EvaluateCase("Rye");   // => "Gay"
x.EvaluateCase("rye");   // => "Gay"
x.EvaluateCase("rYe");   // => "Gay"
x.EvaluateCase("ryE");   // => "Gay"

Forking the repo

  1. Fork it https://github.com/yakovmeister/suicchi/fork
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request