Skip to content

operators

@sandlada/result/operators — data-last curried operators on synchronous IResultOfT.

  • Operates on: IResultOfT<T, E>.
  • Execution model: synchronous; every operator returns a new Result or a plain value.
  • Not here: Option operators (option), eager async (promise-result), lazy thunks (async-result).
Export Description Source
map Transforms the success value. map.ts
mapErr Transforms the error. mapErr.ts
bimap Transforms both variants in one pass. bimap.ts
swap Swaps Ok and Err. swap.ts
flatten Flattens a nested IResultOfT<IResultOfT<T, E>, E>. flatten.ts
filterOrElse Keeps the success value when the predicate passes, otherwise maps it to an error. filterOrElse.ts
Export Description Source
bind Monadic chain: the callback returns the next Result. bind.ts
orElse Recovery: the callback returns a replacement Result on failure. orElse.ts
catchErr Lifts onErr(error) straight into Ok, widening the value channel to A | B. catchErr.ts
and / or Picks the second Result on success / failure. and.ts, or.ts
Export Description Source
contains Equality check against the success value. contains.ts
exists Predicate check on the success value. exists.ts
separate Partitions a list of Results into { ok, err } arrays. separate.ts
traverseArray Maps an array with a Result-returning function; fails fast on the first Err. traverseArray.ts
choose Maps an array and keeps the success values, skipping Errs and continuing. choose.ts
unzip Turns IResultOfT<readonly [A, B], E> into [IResultOfT<A, E>, IResultOfT<B, E>]. unzip.ts
mapOr / mapOrElse Maps the success value or falls back to a default. mapOr.ts, mapOrElse.ts
ap Applies a wrapped function to a wrapped value. ap.ts
Export Description Source
tap / tapErr Synchronous side effects on success / failure; the original Result is returned. tap.ts, tapErr.ts
andTee / orTee Runs a side effect and ignores its result. andTee.ts, orTee.ts
andThrough Runs a chained step but keeps the original success value. andThrough.ts
Export Description Source
match Exhaustive pattern match over both variants. match.ts
unwrapOr / unwrapOrElse Extracts the value, falling back to a default. unwrapOr.ts, unwrapOrElse.ts
Export Description Source
unwrap / expect / unwrapErr / expectErr Panic with a TypeError on contract violation (expect* adds a message). unwrap.ts, expect.ts, unwrapErr.ts, expectErr.ts
unsafeUnwrap / unsafeUnwrapErr Throws the carried value verbatim, without wrapping. unsafeUnwrap.ts, unsafeUnwrapErr.ts
orThrow / orThrowWith Throws a typed error: orThrow requires E extends Error, orThrowWith maps through an error factory first. orThrow.ts
  • Data-last and curried: map(fn)(result); every operator also accepts the direct form map(fn, result).
  • Throw policy is split by family. The map family captures a synchronous callback throw into Err; the bind / mapErr / match / catchErr / unwrapOrElse / mapOr* / exists family propagates directly. choose propagates without capturing and never short-circuits; separate accumulates into partitions; everything else fails fast (FailFirst). See behavior-modes.md §4.2.
  • The escape hatches are three distinct channels: panic (unwrap / expect), raw throw (unsafe*), typed throw (orThrow*). Choose one deliberately.
  • catchErr and orElse both recover, but only catchErr lifts a plain value into Ok; orElse expects a new Result.
  • Standalone curried functions, not methods. Keeping operators off the result objects makes pipe(...) composition natural, avoids prototype mutation, and supports dead-code elimination.
  • option — the parallel operator set on IOption.
  • adapters — bridges between plain functions and Result-returning ones.
  • composition — pipe, composeK, safeTry.