Skip to main content
Module

x/result_js/mod.ts>mapOr

Minimum result type port of Rust
Latest
function mapOr
import { mapOr } from "https://deno.land/x/result_js@2.0.0/mod.ts";

Returns the provided defaultValue (if Err), or applies fn to the contained value (if Ok),

Examples

Example 1

import { Ok, type Result } from "https://deno.land/x/result_js/spec.ts";
import { mapOr } from "https://deno.land/x/result_js/operators/transform.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const result: Result<string, number> = Ok("Hello");
assertEquals(mapOr(result, 0, ({ length }) => length), 5);

Parameters

result: Result<T, unknown>
defaultValue: U
fn: (value: T) => U