mirror of
https://github.com/sickn33/antigravity-awesome-skills.git
synced 2026-04-25 17:25:12 +02:00
* fix: stabilize validation and tests on Windows * test: add Windows smoke coverage for skill activation * refactor: make setup_web script CommonJS * fix: repair aegisops-ai frontmatter * docs: add when-to-use guidance to core skills * docs: add when-to-use guidance to Apify skills * docs: add when-to-use guidance to Google and Expo skills * docs: add when-to-use guidance to Makepad skills * docs: add when-to-use guidance to git workflow skills * docs: add when-to-use guidance to fp-ts skills * docs: add when-to-use guidance to Three.js skills * docs: add when-to-use guidance to n8n skills * docs: add when-to-use guidance to health analysis skills * docs: add when-to-use guidance to writing and review skills * meta: sync generated catalog metadata * docs: add when-to-use guidance to Robius skills * docs: add when-to-use guidance to review and workflow skills * docs: add when-to-use guidance to science and data skills * docs: add when-to-use guidance to tooling and automation skills * docs: add when-to-use guidance to remaining skills * fix: gate bundle helper execution in Windows activation * chore: drop generated artifacts from contributor PR * docs(maintenance): Record PR 457 sweep Document the open issue triage, PR supersedence decision, local verification, and source-only cleanup that prepared PR #457 for re-running CI. --------- Co-authored-by: sickn33 <sickn33@users.noreply.github.com>
92 lines
2.2 KiB
Markdown
92 lines
2.2 KiB
Markdown
---
|
|
name: fp-either-ref
|
|
description: Quick reference for Either type. Use when user needs error handling, validation, or operations that can fail with typed errors.
|
|
risk: unknown
|
|
source: community
|
|
version: 1.0.0
|
|
tags: [fp-ts, either, error-handling, validation, quick-reference]
|
|
---
|
|
|
|
# Either Quick Reference
|
|
|
|
Either = success or failure. `Right(value)` or `Left(error)`.
|
|
|
|
## When to Use
|
|
|
|
- You need a quick fp-ts reference for typed synchronous error handling.
|
|
- The task involves validation, fallible operations, or converting throwing code to `Either`.
|
|
- You want a compact cheat sheet rather than a long tutorial.
|
|
|
|
## Create
|
|
|
|
```typescript
|
|
import * as E from 'fp-ts/Either'
|
|
|
|
E.right(value) // Success
|
|
E.left(error) // Failure
|
|
E.fromNullable(err)(x) // null → Left(err), else Right(x)
|
|
E.tryCatch(fn, toError) // try/catch → Either
|
|
```
|
|
|
|
## Transform
|
|
|
|
```typescript
|
|
E.map(fn) // Transform Right value
|
|
E.mapLeft(fn) // Transform Left error
|
|
E.flatMap(fn) // Chain (fn returns Either)
|
|
E.filterOrElse(pred, toErr) // Right → Left if pred fails
|
|
```
|
|
|
|
## Extract
|
|
|
|
```typescript
|
|
E.getOrElse(err => default) // Get Right or default
|
|
E.match(onLeft, onRight) // Pattern match
|
|
E.toUnion(either) // E | A (loses type info)
|
|
```
|
|
|
|
## Common Patterns
|
|
|
|
```typescript
|
|
import { pipe } from 'fp-ts/function'
|
|
import * as E from 'fp-ts/Either'
|
|
|
|
// Validation
|
|
const validateEmail = (s: string): E.Either<string, string> =>
|
|
s.includes('@') ? E.right(s) : E.left('Invalid email')
|
|
|
|
// Chain validations (stops at first error)
|
|
pipe(
|
|
E.right({ email: 'test@example.com', age: 25 }),
|
|
E.flatMap(d => pipe(validateEmail(d.email), E.map(() => d))),
|
|
E.flatMap(d => d.age >= 18 ? E.right(d) : E.left('Must be 18+'))
|
|
)
|
|
|
|
// Convert throwing code
|
|
const parseJson = (s: string) => E.tryCatch(
|
|
() => JSON.parse(s),
|
|
(e) => `Parse error: ${e}`
|
|
)
|
|
```
|
|
|
|
## vs try/catch
|
|
|
|
```typescript
|
|
// ❌ try/catch - errors not in types
|
|
try {
|
|
const data = JSON.parse(input)
|
|
process(data)
|
|
} catch (e) {
|
|
handleError(e)
|
|
}
|
|
|
|
// ✅ Either - errors explicit in types
|
|
pipe(
|
|
E.tryCatch(() => JSON.parse(input), String),
|
|
E.map(process),
|
|
E.match(handleError, identity)
|
|
)
|
|
```
|
|
|
|
Use Either when **error type matters** and you want to chain operations.
|