@eriveltonsilva/result.js
v1.1.0
Published
A lightweight, Rust-inspired Result type for JavaScript and TypeScript — handle success and error cases without exceptions.
Downloads
294
Maintainers
Readme
Result.js — Rust-inspired Result Type
A lightweight, Rust-inspired Result type for JavaScript and TypeScript — handle success and error cases without exceptions.
📖 Table of Contents
- 1. ✨ Features
- 2. 📦 Installation
- 3. 🚀 Quick Start
- 4. 📚 Documentation
- 5. 🤝 Contributing
- 6. 📝 License
- 7. 🙏 Inspiration
1. ✨ Features
- 🦀 Rust-inspired API - Familiar if you know Rust's
Result<T, E> - 🎯 Type-safe - Full TypeScript support with excellent inference
- 🪶 Zero dependencies - Lightweight and focused
- 🔗 Chainable - Fluent API with
map,andThen, and more - 🧪 Well-tested - Comprehensive test coverage
- 📦 Tree-shakeable - Optimized bundle size
- 🌍 ESM & CommonJS support - Works seamlessly in modern and legacy environments
2. 📦 Installation
npm install @eriveltonsilva/result.jsSupports both ESM (import) and CommonJS (require):
// ESM
import { Result } from '@eriveltonsilva/result.js'
// CommonJS
const { Result } = require('@eriveltonsilva/result.js')3. 🚀 Quick Start
import { Result } from '@eriveltonsilva/result.js'
// Create Results
const success = Result.ok(42)
const failure = Result.err(new Error('Something went wrong'))
// Check state
if (result.isOk()) {
console.log(result.unwrap()) // => 42
}
if (result.isErr()) {
console.log(result.unwrapErr().message) // => 'Something went wrong'
}
// Chain operations
const doubled = Result.ok(21)
.map((x) => x * 2)
.andThen((x) => Result.ok(x + 10))
// Pattern matching
const message = result.match({
ok: (value) => `Success: ${value}`,
err: (error) => `Error: ${error.message}`
})
// Handle promises
const result = await Result.fromPromise(
fetch('/api/data'),
(err) => new NetworkError(err)
)4. 📚 Documentation
For detailed documentation and examples, see the docs directory.
5. 🤝 Contributing
Contributions are welcome! Please read CONTRIBUTING.md for guidelines.
6. 📝 License
MIT © Erivelton Silva - Please read LICENSE.md
7. 🙏 Inspiration
Inspired by:
