0.0.5 • Published 5 years ago

suitchi v0.0.5

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

suitchi

Switch expressions in JavaScript.

Usage

suitchi(expression, mappings)

ParameterTypeDescription
expressionanyThe expression to match against the cases in the mappings.
mappings[RegularCase, ..., DefaultCase?]A list of cases to match against to expression.

RegularCase: [Case, Value]

DefaultCase: Value

Entries in mappings are iterated over until a match is found.

A RegularCase matches if its Case matches expression. The DefaultCase, if it exists, always matches and must be the final entry of mappings

When a match is found, the processed value of the corresponding Value is returned.

Case

If a function, it is called with expression and matches if it returns true.

If not a function, it matches if its value is exactly equal to expression.

Value

If a function, its processed value is its return value when called.

If not a function, its processed value is itself.

Examples

const userInput = "yes";
const reply = suitchi(userInput, [
    ["yes", "Coolbeans."],
    ["no", "Aw, man!"],
    "Please input an answer.", // default value
]);
// reply === "Coolbeans."
suitchi(userInput, [
    ["", "Please input something."],
    () => {
        const spaced = userInput + " ";
        const rep = spaced.repeat(3);
        return rep.slice(0, -1);
    },
]);
const sixteenIsEven = suitchi(16, [
    [n => n % 2 === 0, true],
    false,
]);
0.0.5

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.0

6 years ago