@knownasrazi/resultify
v1.0.0
Published
Result<T, E> type with map, flatMap, unwrap and error chaining for zero-pain error handling.
Maintainers
Readme
resultify
Result<T, E> for zero-pain error handling.
Zero dependencies - ~2 KB gzipped - TypeScript - Node + Bun + browser
Install
bun add @knownasrazi/resultify
npm install @knownasrazi/resultifyUsage
import { ok, err, tryCatch, Result } from "@knownasrazi/resultify";
const r = ok(2).map(n => n * 3);
r.unwrap(); // 6
err(new Error("boom")).map(n => n * 2); // still err
tryCatch(() => JSON.parse(maybeJson));
Result.tryCatch(() => risky(), e => new Error(String(e)));
ok(5).flatMap(n => n > 10 ? err(new Error("too big")) : ok(n + 1))
.match(v => console.log("ok", v), e => console.error(e));API
| Method | Description |
| --- | --- |
| ok(value) / err(error) | Constructors |
| tryCatch(fn, catcher?) | Capture throws as Result |
| .map(fn) / .mapErr(fn) | Transform ok/err |
| .flatMap(fn) | Chain Result-returning fns |
| .unwrap() / .unwrapOr(v) / .unwrapOrElse(fn) | Extract |
| .match(ok, err?) | Dispatch |
Development
git clone https://github.com/knownasrazi/resultify.git
cd resultify
bun install
bun test
bun run build
bun run lintLicense
resultify - errors as values, not exceptions.
