@omgimalexis/switch-case

<h1 align="center">Welcome to switch-case 👋</h1> <p> <a href="https://www.npmjs.com/package/switch-case" target="_blank"> <img alt="Version" src="https://img.shields.io/npm/v/switch-case.svg"> </a> <a href="#" target="_blank"> <img alt="Lic

Usage no npm install needed!

<script type="module">
  import omgimalexisSwitchCase from 'https://cdn.skypack.dev/@omgimalexis/switch-case';
</script>

README

Welcome to switch-case 👋

Version License: MIT Twitter: OmgImAlexis

A functional approach to switches

Install

npm install @omgimalexis/switch-case

Usage

const switchCase = require('switch-case').default;

// Returns selected case
{
  const case = 'a';
  const result = await switchCase({
    a: 1,
    b: 2,
    default: 3
  }, case);

  // result = 1
}

// Returns default case when selected isn't found
{
  const case = 'z';
  const result = await switchCase({
    a: 1,
    b: 2,
    default: 3
  }, case);

  // result = 3
}

// Allows functions
{
  const case = 'a';
  const result = await switchCase({
    a: () => 1,
    b: 2,
    default: 3
  }, case);

  // result = 1
}

// Allows async functions
{
  const case = 'a';
  const result = await switchCase({
    a: async () => 1,
    b: 2,
    default: 3
  }, case);

  // result = 1
}

Run tests

npm run test

Author

👤 Alexis Tyler xo@wvvw.me (https://wvvw.me/)

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!