Welcome to the Switch Functional repository! This project offers a functional switch statement for JavaScript and TypeScript. It provides a clean and efficient way to handle multiple cases in your code.
The switch statement is a powerful tool in programming. However, its traditional use can sometimes lead to complex and hard-to-read code. This library simplifies the switch-case logic by adopting a functional approach. It allows you to map functions directly to cases, making your code cleaner and more maintainable.
- Functional Approach: Emphasizes the use of functions for case handling.
- Easy to Use: Simple syntax that integrates well with existing code.
- Supports Default Cases: Easily define default behavior when no cases match.
- TypeScript Support: Fully compatible with TypeScript for type safety.
- Lightweight: Minimal footprint for fast performance.
To install the library, use npm or yarn:
npm install switch-functional
or
yarn add switch-functional
Once installed, you can import it into your project:
import { switchCase } from 'switch-functional';
The switchCase
function allows you to define your cases and corresponding actions in a clear manner. Here's a simple structure:
const result = switchCase(value)
.case('case1', () => 'Result for case 1')
.case('case2', () => 'Result for case 2')
.default(() => 'Default result');
This approach enhances readability and maintainability, especially in larger applications.
Hereβs a basic example of how to use the switchCase
function:
import { switchCase } from 'switch-functional';
const day = 'Monday';
const message = switchCase(day)
.case('Monday', () => 'Start of the week!')
.case('Friday', () => 'Almost weekend!')
.default(() => 'Just another day.');
console.log(message); // Output: Start of the week!
You can also use more complex logic within your cases:
import { switchCase } from 'switch-functional';
const action = 'DELETE';
const response = switchCase(action)
.case('CREATE', () => 'Creating a resource...')
.case('UPDATE', () => 'Updating a resource...')
.case('DELETE', () => {
// Some complex logic
return 'Deleting a resource...';
})
.default(() => 'Unknown action.');
console.log(response); // Output: Deleting a resource...
For TypeScript users, the library provides type safety:
import { switchCase } from 'switch-functional';
type Action = 'CREATE' | 'UPDATE' | 'DELETE';
const action: Action = 'CREATE';
const response = switchCase(action)
.case('CREATE', () => 'Creating a resource...')
.case('UPDATE', () => 'Updating a resource...')
.case('DELETE', () => 'Deleting a resource...')
.default(() => 'Unknown action.');
console.log(response); // Output: Creating a resource...
We welcome contributions! If you would like to help improve this library, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Make your changes.
- Commit your changes (
git commit -m 'Add new feature'
). - Push to the branch (
git push origin feature/YourFeature
). - Open a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.
For questions or feedback, please reach out:
- GitHub: CRYPTONIXT
- Email: cryptonix@example.com
Feel free to visit the Releases section for the latest updates and downloads.
Happy coding! π