Skip to content

Commit 40f6d8f

Browse files
committed
feat(unwrapErrOrElse): add unwrapErrOrElse function
1 parent 22bb50e commit 40f6d8f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,20 @@ export function unwrapErrOrFn<const TDefault>(
187187
): <const TReason>(result: Result<unknown, TReason>) => TReason | TDefault {
188188
return result => unwrapErrOr(result, defaultReason);
189189
}
190+
191+
export function unwrapErrOrElse<const TValue, const TInReason, const TOutReason>(
192+
result: Result<TValue, TInReason>,
193+
elseFn: (value: TValue) => TOutReason
194+
): TInReason | TOutReason {
195+
if (result instanceof Ok) {
196+
return elseFn(result.value);
197+
} else {
198+
return result.reason;
199+
}
200+
}
201+
202+
export function unwrapErrOrElseFn<const TValue, const TOutReason>(
203+
elseFn: (value: TValue) => TOutReason
204+
): <const TInReason>(result: Result<TValue, TInReason>) => TInReason | TOutReason {
205+
return result => unwrapErrOrElse(result, elseFn);
206+
}

0 commit comments

Comments
 (0)