Switch/case in functional programming style
npm install switch-in-fpswitch-in-fpswitch-case implementation for JavaScript. Because why not?
switch-case implementation using the if library. It's like a regular switch-case, but with extra steps. Perfect for those who want to feel smarter while writing code that does the same thing as a plain old switch.
null, undefined, empty strings, arrays, objects, and even negative numbers. (Why? I don't know either.)
setTimeout, and setInterval. Because blocking the event loop is so last year.
.else() method for handling unmatched cases.
bash
npm install switch-in-fp
`
Or, if you're feeling adventurous:
`bash
yarn add switch-in-fp
`
---
๐ ๏ธ Usage
Hereโs how you can use this package to make your code unnecessarily complex:
`javascript
const { SwitcherFactory } = require('switch-in-fp');
Switch(42)
.case(42, (v) => console.log(v))
.case(43, (v) => console.log(v))
.else(() => console.log('No matching case'))
.execute();
// Output: 42
const result = Switch(42)
.case(42, (v) => v * 2)
.execute();
console.log(result); // Output: 84
`
Advanced Example
`javascript
Switch(7)
.case(7, () => {
throw new Error('Something went wrong');
})
.else(() => console.log('Default action'))
.execute();
// Logs the error and continues execution
`
Asynchronous Actions
`javascript
Switch(6)
.case(6, async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log('Async action executed');
})
.execute();
// Waits 1 second, then logs "Async action executed"
`
---
๐งช Tests
Run the tests to ensure everything works as expected:
`bash
npm test
``