@el3um4s/match
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

@el3um4s/match

Alternative to JavaScript’s switch statement with a functional twist

Based on Alternative to JavaScript’s switch statement with a functional twist

NPM: @el3um4s/match

Install and use the package

To use the package in a project:

npm i @el3um4s/match

and then in a file:

import match from "@el3um4s/match";

const handleShape = (shape, w) => area(shape,w);

function area (type = "Circle", width = 2) {
  return match(type)
          .on(type => type.toLowerCase() === "circle", () => {
              return Math.PI * (width / 2) ** 2;
            })
          .on(type => type.toLowerCase() === "square", () => width ** 2)
          .otherwise(() => 0);
};

handleShape("Square", 3);

Package Sidebar

Install

npm i @el3um4s/match

Weekly Downloads

2

Version

0.0.5

License

MIT

Unpacked Size

4.46 kB

Total Files

5

Last publish

Collaborators

  • el3um4s