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
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
P extends keyof A
Parameters
prop: P