Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/el3um4s/match
Alternative to JavaScript’s switch statement with a functional twist
https://github.com/el3um4s/match
functional-programming javascript js npm npm-package switch switch-case ts typescript
Last synced: 12 days ago
JSON representation
Alternative to JavaScript’s switch statement with a functional twist
- Host: GitHub
- URL: https://github.com/el3um4s/match
- Owner: el3um4s
- License: mit
- Created: 2021-09-22T12:18:39.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-08T17:43:58.000Z (almost 2 years ago)
- Last Synced: 2024-10-11T12:53:27.812Z (about 1 month ago)
- Topics: functional-programming, javascript, js, npm, npm-package, switch, switch-case, ts, typescript
- Language: TypeScript
- Homepage:
- Size: 771 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# @el3um4s/match
Alternative to JavaScript’s switch statement with a functional twistBased on [Alternative to JavaScript’s switch statement with a functional twist](https://codeburst.io/alternative-to-javascripts-switch-statement-with-a-functional-twist-3f572787ba1c)
NPM: [@el3um4s/match](https://www.npmjs.com/package/@el3um4s/match)
### Install and use the packageTo use the package in a project:
```bash
npm i @el3um4s/match
```and then in a file:
```ts
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);
```