@fxwss/result
v1.0.1
Published
A TypeScript Result monad library for error handling
Maintainers
Readme
@fxwss/result
A TypeScript Result monad library for elegant error handling without exceptions.
Installation
npm install @fxwss/result
# or
yarn add @fxwss/result
# or
bun add @fxwss/resultUsage
import { Result, ok, err } from "@fxwss/result";
// Create successful results
const success = ok(42);
const failure = err("Something went wrong");
// Chain operations safely
const result = ok(10)
.map((x) => x * 2)
.map((x) => x + 1)
.unwrapOr(0); // Returns 21
// Handle errors gracefully
function divide(a: number, b: number): Result<string, number> {
if (b === 0) {
return err("Division by zero");
}
return ok(a / b);
}
const safeResult = divide(10, 2)
.map((x) => x * 2)
.unwrapOr(0); // Returns 10API
Creating Results
ok(value)- Create a successful Resulterr(error)- Create an error Result
Methods
map(fn)- Transform the success valueunwrap()- Get the value (throws on error)unwrapOr(default)- Get the value or return defaultisOk()- Check if result is successfulisErr()- Check if result is an error
Development
# Install dependencies
bun install
# Run tests
bun test
# Build the library
npm run buildLicense
MIT
