minifunc

Lens

A lens is a functional way to focus on a specific part of a data structure. It is similar to a getter and setter, but it is immutable.

Initialization

import { Lens } from 'minifunc';

const lens = Lens('Name of the field you want to target');

Notes

API

Lens<T, U>

type AnythingWith<Field extends string | number | symbol, T> = {
	[_ in Field]: T;
};

interface Lens<T extends string | number | symbol, I> {
	get<U extends AnythingWith<T, I>>(obj: U): I;
	maybeGet<U>(obj: U): I | undefined;
	set<U>(obj: U, value: I): U;
	change<U extends AnythingWith<T, I>, I>(obj: U, value: I): U;
	map<U extends AnythingWith<T, I>, J>(obj: U, f: (old: I) => J): U;
}