@onrails/maybe
v0.1.2
Published
Tagged Maybe for expected absence — pure tagged unions, Result interop, tree-shakeable
Maintainers
Readme
@onrails/maybe
Tagged Maybe for expected absence — cache miss, optional field, not-ready-yet. Not for failures (use @onrails/result).
Install
bun add @onrails/maybeUsage
import { compactMap, fromNullable, isSome, match, some } from "@onrails/maybe";
const row = fromNullable(db.get(id));
const name = match(
row,
(r) => r.name,
() => "guest",
);Side effects
tap runs an effect on the Some value, tapNone on absence — both pass the
Maybe through unchanged. Mirrors tap/tapErr from @onrails/result. Dual-form.
import { fromNullable, tap, tapNone } from "@onrails/maybe";
const row = fromNullable(db.get(id));
tap(row, (r) => log.debug("hit", r.id)); // runs on Some, returns row
tapNone(row, () => metrics.miss()); // runs on None, returns rowResult boundary
import { toResult } from "@onrails/maybe/interop";
const user = toResult(fromNullable(row), () => ({ kind: "not_found" as const }));Subpaths
| Path | Contents |
|------|----------|
| @onrails/maybe | Core |
| @onrails/maybe/fluent | fluent(maybe) chains |
| @onrails/maybe/interop | toResult, fromResult, NoneError |
See DESIGN.md.
