Skip to main content
Module

x/fun/mod.ts>optic.atKey

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
function optic.atKey
import { optic } from "https://deno.land/x/fun@v2.0.0/mod.ts";
const { atKey } = optic;

A composible combinator that focuses on a key in a readonly record. The difference between atKey and key is that the key can be removed from the record by the modify function if the modify function returns None.

Examples

Example 1

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

const atOne = pipe(
  O.id<Readonly<Record<string, string>>>(),
  O.atKey("one"),
);
const removeAtOne = pipe(atOne, O.replace(constNone()));

const result1 = pipe(atOne, O.view({})); // None
const result2 = pipe(atOne, O.view({ one: "one" })); // Some("one")
const result3 = removeAtOne({}); // {}
const result4 = removeAtOne({ one: "one" }); // {}
const result5 = removeAtOne({ one: "one", two: "two" }); // { two: "two" }

Parameters

key: string

Returns

<U extends Tag, S, A>(first: Optic<U, S, Readonly<Record<string, A>>>) => Optic<Align<U, LensTag>, S, Option<A>>