suicchi
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
; const switchCase = ; switchCase;switchCase;switchCase; const name = switchCase; console; // => "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
; const defaultCase = "no-record"; const switchCase = defaultCase; switchCase;switchCase;switchCase; const age = switchCase; console; // => "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
; const switchCase = ; switchCase;switchCase;switchCase; const car = switchCase; console; // 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 = { // DO OTHER THINGS... return 1 + 1;};const aSwitch = 'aValue';const bSwitch = 'anotherValue';
You'll be doing something equivalent to:
// Switch // aSwitch // bSwitch
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 = default: null; case1: "What"; 1234
You'll be doing something similar to:
// cSwitch
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 = ; // To assign a single case to a single routinex; // the above is equivalent to: // To assign a multiple cases to a single routinex; // the above is equivalent to: // To assign multiple cases to multiple routinesx; // the above is equivalent to:
GetCases
The GetCases method will return a string array containing the existing cases that you've set for the Suicchi instance.
const x = default: null; case1: "What"; 1234 x; // => ['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 = default: "Awesome" Rye: "Gay" rye: "Gay" rYe: "Gay" ryE: "Gay"; x; // => "Awesome"x; // => "Awesome"x; // => "Gay"x; // => "Gay"x; // => "Gay"x; // => "Gay"
Forking the repo
- Fork it https://github.com/yakovmeister/suicchi/fork
- Create your feature branch (git checkout -b feature/fooBar)
- Commit your changes (git commit -am 'Add some fooBar')
- Push to the branch (git push origin feature/fooBar)
- Create a new Pull Request