Skip to main content
Module

x/fun/optic.ts>prop

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

A composable combinator that focuses on a property P of a struct.

Examples

Example 1

import * as O from "./optic.ts";
import { toUpperCase } from "./string.ts";
import { pipe } from "./fn.ts";

type Person = { name: string, age: number };

const person = O.id<Person>();
const name = pipe(person, O.prop("name"));
const age = pipe(person, O.prop("age"));

const brandon: Person = { name: "Brandon", age: 37 };
const emily: Person = { name: "Emily", age: 35 };

const result1 = pipe(name, O.view(brandon)); // "Brandon"
const result2 = pipe(name, O.view(emily)); // "Emily"
const result3 = pipe(age, O.view(brandon)); // 37
const result4 = pipe(brandon, name.modify(toUpperCase));
// { name: "BRANDON", age: 37 }

Type Parameters

A
P extends keyof A

Returns

<U extends Tag, S>(sa: Optic<U, S, A>) => Optic<Align<U, LensTag>, S, A[P]>