@kazumatu981/whenon
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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);
}

Readme

Keywords

none

Package Sidebar

Install

npm i @kazumatu981/whenon

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

10.7 kB

Total Files

9

Last publish

Collaborators

  • kazumatu981