switch-set

Define variables with switches.

Usage no npm install needed!

<script type="module">
  import switchSet from 'https://cdn.skypack.dev/switch-set';
</script>

README

switch-set

Define variables with switches.

NPM version

Install

NPM

> npm i switch-set

Yarn

> yarn add switch-set

Examples

It handles all types, and executes functions.

import { set } from "switch-set";

const options = ["one", "two", "three", "four"];
const choice = options[Math.floor(Math.random() * options.length)];

const res = assign(choice, {
    "one": "Picked One!",
    "two": () => "Picked Two!",
    "three": 3,
    default: function(input: any) {
        return `${input} was not a handled case.`
    }
})

console.log(res)

Async Promise Support

Handles 'ok' on resolve, and 'err' on reject.

import { set } from "switch-set";

let promise = new Promise((reject) => setTimeout(() => {reject("I Rejected!")}, 2000));

const res = await assign(promise, {
    ok: (res: any) => `Resolved with '${res}'!`,
    err: (err: any) => `Rejected with '${err}'!`
});

console.log(res) // " Rejected with 'I rejected!' "

License

MIT