Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fun/option.ts>mapNullable

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
function mapNullable
import { mapNullable } from "https://deno.land/x/fun@v.2.0.0-alpha.11/option.ts";

Apply a mapping function to an Option but if the mapping function returns null or undefined the null or undefined value is lifted into None.

Examples

Example 1

import * as O from "./option.ts";
import { pipe } from "./fn.ts";

const result1 = pipe(
  O.some([1, 2, 3]),
  O.mapNullable(arr => arr[10]),
); // None
const result2 = pipe(
  O.constNone<Array<number>>(),
  O.mapNullable(arr => arr[0]),
); // None
const result3 = pipe(
  O.some([1, 2, 3]),
  O.mapNullable(arr => arr[0]),
); // Some(1)

Parameters

f: (a: A) => I | null | undefined

Returns

(ua: Option<A>) => Option<I>