@kazumatu981/whenon
v1.0.0
Published
Readable Mutli-Bifurcation interface.
Downloads
2
Readme
whenon
this project is under constructed now.... and not registered on
npm
Readable multi-branch processing interface.
Concept
I thought that it is not so elegant to write multi-baranch processing to be written in switch-case syntax.
🤢Ugly code style
For example......
const food = getFood();
let animal = "unknown";
switch(food) {
case "banana":
animal = "monkey";
break;
case "chicken":
animal = "crocodile";
break;
case "bread":
animal = "human";
break;
}
feedTo(animal);
NOT SO elegant !!!!
😊Elegant code sytle
And then I hope to write elegant syntax like bellow.
const animal = when(getFood)
.on("banana", "monkey")
.on("chicken", "crocodile")
.on("bread", "human")
.otherwise("unknown");
feedTo(animal);
Yah! It's smarter!! How do you think about? If you feel so good, please use this libary.
How to use
🚀Let's get started
first install this libray
npm install -save @kazumatu981/whenon
import this libray
const { when } = require('@kazumatu981/whenon');
or
import { when } form '@kazumatu981/whenon';
✒️re-write your code.
constant switch style
const animal = when(getFood)
.on("banana", "monkey")
.on("chicken", "crocodile")
.on("bread", "human")
.otherwise("unknown");
feedTo(animal);
lambda expression style
const bmi = calcBMI(hight, weight, age);
const obesity = when(bmi)
.on(b => b < 18.5, "thin")
.on(b => 18.5 <= b && b < 25, "normal")
.on(b => 25.0 <= b && b < 30, "warning to fat")
.on(b => 30 <= b, "fat")
.resolved;
lazy evaluation style
const combolution = when
.on(x => 0 <= x && x < 0.5, x => 2 * x)
.on(0.5 <= x && x <=1, x => 2 * (1 - x))
.otherwize(_=> throw new Error("Out of Range."));
for(let i = 0, x = 0.3; i<100; i++) {
console.log(`x = ${x}`);
x = combolution(x);
}