switch-set
v1.0.1
Published
Define variables with switches.
Downloads
3
Readme
switch-set
Define variables with switches.
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