@bulatlib/res
v0.0.19
Published
Rust style Result for TypeScript
Downloads
237
Readme
@bulatlib/res — Rust‑style Result for TypeScript
Small, strict utilities for Rust‑style (Ok/Err) results. Provides safe constructors, execution wrappers, and transformation chains via pipe.
Install
npm i @bulatlib/res
# or
pnpm add @bulatlib/res
# or
bun add @bulatlib/resQuick API
Core types
Res<T>: union{ ok: T; err?: never } | { ok?: never; err: Error }.
Constructors
ok(value)— create a successful result.err(error)— create an error result.
Safe execution
wrap(fn)— run a sync function with try/catch and returnRes<T>.wrapAsync(fn)— run an async function with try/catch and returnPromise<Res<T>>.
Pipe
pipe.from(res)— wrapsRes<T>and returnsPipe<T>with chainable methods.Pipe<T> methods:map(fn)—Ok(T) -> Ok(fn(T)),Errunchanged.mapErr(fn)— transformErrortoErr(fn(err)).mapOr(default, fn)— onOkreturnOk(fn(T)), onErrreturnOk(default).mapOrElse(defaultFn, fn)— onOkreturnOk(fn(T)), onErrreturnOk(defaultFn(err)).and(res)— ifOk, return the secondres, otherwise keep originalErr.andThen(fn)— ifOk, evaluatefn(T): Res<U>, otherwiseErr.or(res)— ifOk, return it; otherwise return the secondres.orElse(fn)— ifErr, evaluatefn(err): Res<T>; otherwise keep originalOk.unwrapOr(default)— returnTordefault.unwrapOrElse(fn)— returnTorfn(err).match({ ok, err })— pattern match and return a value of one of callbacks.res()— return rawRes<T>.
Utilities
combine(results)— foldRes<T>[]intoRes<T[]>. Returns the firstErrif any, otherwiseOkwith collected values in order.
Examples
Examples will be added soon. Meanwhile, please check the test files in src (e.g., src/*.test.ts) for usage examples.
License
MIT
