Skip to main content

cond(predicate): PartialLens

ts
cond: <A>(predicate: (a: A) => boolean) => PartialLens<A, A>;
ts
cond: <A>(predicate: (a: A) => boolean) => PartialLens<A, A>;

This combinators takes a predicate and, when passed to derive, returns a new partial optic focused either on the original value or undefined if the predicate returns false.

Example:

ts
import { cond } from "@optics/react/combinators";
 
const numberOptic = createState(42);
 
const evenNumberOptic = numberOptic.derive(cond((n) => n % 2 === 0));
const evenNumberOptic: Optic<number, partial>
 
evenNumberOptic.get(); // 42
 
evenNumberOptic.set((prev) => prev + 1);
 
evenNumberOptic.get(); // undefined
numberOptic.get(); // 43
ts
import { cond } from "@optics/react/combinators";
 
const numberOptic = createState(42);
 
const evenNumberOptic = numberOptic.derive(cond((n) => n % 2 === 0));
const evenNumberOptic: Optic<number, partial>
 
evenNumberOptic.get(); // 42
 
evenNumberOptic.set((prev) => prev + 1);
 
evenNumberOptic.get(); // undefined
numberOptic.get(); // 43