switch-case

2.1.5 • Public • Published

IMPORTANT

This package is not maintained any more. See https://github.com/OmgImAlexis/switch-case for an alternative please (thanks to @omgimalexis)

switch-case

A switcher for complex and dynamic cases.

NPM version build status Test coverage NPM downloads Node.js dependencies

Install

$ npm install --save switch-case

Get started

Use it just like switch case

const Switcher = require('switch-case');
const switcher = new Switcher();

switcher
    .case(1, () => {
        console.log(1);
    })
    .case(2, () => {
        console.log(2);
    });


switcher.switch(1);   // ===> prints 1 on the screen

Why

For most cases the native syntax switch case is adequate, if you know every case well. But if your cases are not so clear or just dynamic during your program running time, you may need this.

For me, I use this module for a router module, which I would never want to write with native siwtch case syntax.

async handlers

This module supports async handlers in cases.

const result = await switcher.case(1, async () => {
    await sleep(100);
    return 100;
});

Custom proxy

You can change the behavior by providing a proxy:

const switcher = new Switcher(proxy);

While these properties as expected in the proxy:

proxy.match(targetCondition, condition)

Checks if the target condition passed by switcher.switch matched the case condition. If true returned, the result will be executed.

proxy.execute(result, targetCondition)

Executes the result with the target condition as a param. Support both async or sync functions.

proxy.validateCondition(condition)

Check if the condition is valid. If not, an error will be thrown.

proxy.validateResult(result)

Check if the result is valid. If not, an error will be thrown.

proxy.createIndex(condition, index, indexTable)

Create an index on the condition, and store it on the index table.

proxy.searchIndex(condition, indexTable)

Search an index from the index table. Should return the index number list. If false returned, means index not found, the switcher will scan all cases to match.

extend switcher into a router

see lark-router

LICENCE

MIT

Readme

Keywords

Package Sidebar

Install

npm i switch-case

Weekly Downloads

14

Version

2.1.5

License

MIT

Unpacked Size

18.9 kB

Total Files

10

Last publish

Collaborators

  • viringbells