aboutsummaryrefslogtreecommitdiff
path: root/utils/result/map.ts
blob: 6d8697e09b0823f915312f571406e666ab2f8f75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
import { err, ok } from '@hollowdark/utils/result/constructors'
import type { Result } from '@hollowdark/utils/result/types'

/** Map the value of an ok result; pass errors through unchanged. */
export function mapResult<T, U, E>(r: Result<T, E>, f: (value: T) => U): Result<U, E> {
  return r.ok ? ok(f(r.value)) : r
}

/** Map the error of a failed result; pass values through unchanged. */
export function mapErr<T, E, F>(r: Result<T, E>, f: (error: E) => F): Result<T, F> {
  return r.ok ? r : err(f(r.error))
}