Guide
Welcome to the @anilkumarthakur/match documentation. This guide will help you understand and use the library effectively.
What is @anilkumarthakur/match?
@anilkumarthakur/match is a JavaScript/TypeScript library that brings the power and elegance of PHP's match expression to the JavaScript world. It provides a clean, type-safe alternative to complex switch statements and nested if-else logic.
Key Features
- Type-Safe: Full TypeScript support with generic types
- Readable: Clean, expressive syntax
- Fast: O(1) lookup performance using JavaScript's Map
- Lightweight: Zero dependencies, ~1.2KB gzipped
- Well-Tested: 245 comprehensive tests with 100% code coverage
- Chainable: Fluent API for method chaining
Why Use @anilkumarthakur/match?
Traditional Switch Statement
javascript
const getStatus = (code) => {
switch (code) {
case 200:
case 201:
case 202:
return 'Success'
case 400:
case 401:
case 403:
return 'Client Error'
case 500:
return 'Server Error'
default:
return 'Unknown'
}
}With @anilkumarthakur/match
typescript
import { match } from '@anilkumarthakur/match'
const getStatus = (code: number) => {
return match(code)
.onAny([200, 201, 202], () => 'Success')
.onAny([400, 401, 403], () => 'Client Error')
.on(500, () => 'Server Error')
.otherwise(() => 'Unknown')
}Advantages
- More Readable: The intent is clearer and easier to follow
- Type-Safe: Full TypeScript support catches errors at compile time
- Less Error-Prone: No fall-through cases or missing breaks
- Composable: Easy to combine with other functional patterns
- Testable: Each case is independent and easy to test
Next Steps
- Installation - How to install the library
- Quick Start - Get up and running in 5 minutes
- Basic Usage - Learn the core concepts
- Advanced Patterns - Master advanced techniques