switch-set
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i switch-set

Weekly Downloads

4

Version

1.0.1

License

MIT

Unpacked Size

9.31 kB

Total Files

6

Last publish

Collaborators

  • blairspalace