Package Exports
- switch-in-fp
- switch-in-fp/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (switch-in-fp) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
switch-in-fp

A functional programming-inspired
switch-caseimplementation for JavaScript. Because why not?
π€ What is this?
This package provides a functional programming-style 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.
π Features
- Functional Programming Vibes: Adds unnecessary complexity to your codebase.
- Duplicate Case Handling: Automatically ignores duplicate cases because i'm too lazy to throw errors.
- Edge Case Support: Handles
null,undefined, empty strings, arrays, objects, and even negative numbers. (Why? I don't know either.) - Error Handling: Catches errors in actions and logs them to the console. You're welcome.
- Asynchronous Actions: Supports async functions, Promises,
setTimeout, andsetInterval. Because blocking the event loop is so last year. - Default Action: Provides an
.else()method for handling unmatched cases.
π¦ Installation
Install the package via npm:
npm install switch-in-fpOr, if you're feeling adventurous:
yarn add switch-in-fpπ οΈ Usage
Hereβs how you can use this package to make your code unnecessarily complex:
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: 84Advanced Example
Switch(7)
.case(7, () => {
throw new Error('Something went wrong');
})
.else(() => console.log('Default action'))
.execute();
// Logs the error and continues executionAsynchronous Actions
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:
npm testπ License
This project is licensed under the MIT License. Do whatever you want with it, but don't blame us if it breaks your codebase.
β οΈ Warning
If you find yourself using this package to replace in real project a simple switch-case, please stop it. Get some help.
π Contributing
Feel free to contribute! Or don't. I'm not your boss or mom.
π Feedback
If you have any feedback (or say what i'm a stupid), suggestions, or complaints, feel free to open an issue. Or donβt. Itβs up to you.