Skip to content

A functional switch statement that enhances code clarity and efficiency. 🌟 Use it to streamline your decision-making processes in JavaScript. πŸ’»

License

Notifications You must be signed in to change notification settings

CRYPTONIXT/switch-functional

Repository files navigation

Switch Functional 🌟

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.

Download Releases

Table of Contents

Introduction

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.

Features

  • 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.

Installation

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';

Usage

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.

Examples

Basic Example

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!

Complex Example

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...

TypeScript Example

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...

Contributing

We welcome contributions! If you would like to help improve this library, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes.
  4. Commit your changes (git commit -m 'Add new feature').
  5. Push to the branch (git push origin feature/YourFeature).
  6. Open a Pull Request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For questions or feedback, please reach out:

Feel free to visit the Releases section for the latest updates and downloads.

Happy coding! πŸŽ‰

About

A functional switch statement that enhances code clarity and efficiency. 🌟 Use it to streamline your decision-making processes in JavaScript. πŸ’»

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •