aboutsummaryrefslogtreecommitdiff
path: root/utils/result/unwrap.ts
blob: 8f980dd196544e72b82da6903728921849ec34b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import type { Result } from '@hollowdark/utils/result/types'

/**
 * Extract the value from an ok result; throw on error. Reserve for tests
 * and assertion contexts — in production code prefer branching on isOk.
 */
export function unwrap<T, E>(r: Result<T, E>): T {
  if (!r.ok) {
    throw new Error(`unwrap() called on error result: ${String(r.error)}`)
  }
  return r.value
}

/** Extract the value or return a fallback. */
export function unwrapOr<T, E>(r: Result<T, E>, fallback: T): T {
  return r.ok ? r.value : fallback
}