1.0.1 • Published 4 years ago

switch-set v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

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