Forked from lens.ts since I couldn't contact the original author for fixes.
To install, use
npm install @rakeshpai/lens.ts
Original readme follows.
lens.ts 
TypeScript Lens implementation with property proxy
Lens?
Lens is composable abstraction of getter and setter. For more detail of Lens, I
recommend reading the following documents.
Install
Via npm:
npm i lens.ts
Usage
import { lens } from 'lens.ts';
type Person = {
name: string;
age: number;
accounts: Array<Account>;
};
type Account = {
type: string;
handle: string;
};
let azusa: Person = {
name: 'Nakano Azusa',
age: 15,
accounts: [
{
type: 'twitter',
handle: '@azusa'
},
{
type: 'facebook',
handle: 'nakano.azusa'
}
]
};
let personL = lens<Person>();
personL.k('name')
personL.k('accounts')
personL.k('hoge')
personL.k('accounts').k(1)
personL.k(1)
personL.name
personL.accounts
personL.accounts[1]
personL.hoge
personL.accounts[0].handle.get()(azusa)
personL.accounts[0].handle.set('@nakano')(azusa)
personL.age.set(x => x + 1)(azusa)
let fstAccountL = lens<Person>().accounts[0]
let handleL = lens<Account>().handle
fstAccountL.compose(handleL)
fstAccountL.get(handleL.get())(azusa)
fstAccountL.set(handleL.set('@nakano'))(azusa)
You can find similar example code in /test.
API
lens.ts
exports the followings:
import {
lens,
Getter,
Setter,
Lens
} from 'lens.ts';
lens
A function lens
is a factory function for a lens. Without any arguments
except for a type parameter, it returns an identity lens for the provided
type.
lens<Person>()
You can provide a getter and a setter to create a lens manually. They should
have proper Getter
and Setter
types.
let getter
let setter
lens(getter, setter)
Getter
, Setter
They are just a type alias for the following function types.
export type Getter<T, V> = (target: T) => V;
export type Setter<T> = (target: T) => T;
Basically, Getter
is a function to retrieve a value from a target. Setter
is
a function to set or update a value in a provided target and return a new object
with a same type as the target.
Any Setter
returned from Lens
has immutable API, which means it doesn't
modify the target object.
Lens<T, U>
A lens is consisted of a getter and a setter for a source type T
and a
result type U
.
Lens
is not a class, so it can't be created with new Lens()
. It's
internally a product type of LensImpl
and LensProxy
. Please use lens()
to create a lens.
Lens
provides the following methods.
.k<K extends keyof U>(key: K)
Narrow the lens for a property or an index of U
.
type Person = {
name: string;
age: number;
accounts: Account[];
};
lens<Person>().k('name')
lens<Person>().k('accounts')
lens<Person>().k('accounts').k(0)
.get()
It is polymorphic.
.get(): Getter<T, U>
.get<V>(f: Getter<U, V>): Getter<T, V>
.get()
returns a getter, which can be applied to an actual target object to
retrive an actual value. You can optionally provide another getter (or mapping
function) to retrieve a mapped value.
let target = { age: 15, ... };
let ageL = lens<Person>().k('age');
ageL.get()(target)
ageL.get(age => age + 10)(target)
.set()
It is polymorphic.
.set(val: U): Setter<T>
.set(f: Setter<U>): Setter<T>
.set()
returns a setter, which can set or update an internal value and returns
an updated (and new) object. Setter
s here should be all immutable. You can
provide a value to set, or optionally a setter for value.
let target = { age: 15, ... };
let ageL = lens<Person>().k('age');
ageL.set(20)(target)
ageL.set(age => age + 1)(target)
.compose(another: Lens<U, V>): Lens<U, V>
Compose 2 lenses into one.
let lens1: Lens<T, U>;
let lens2: Lens<U, V>;
let accountsL = lens<Person>().k('accounts');
let firstL = <T>() => lens<T[]>().k(0);
let firstAccountL =
accountsL.compose(firstL());
FYI: The reason firstL
becomes a function with <T>
is to make it
polymorphic.
Proxied properties
Lens<T, U>
also provides proxied properties for the type U
.
objL.name
arrL[0]
Note that proxy objects are not available in Internet Explorer as caniuse describes.
Credits
Property proxy couldn't have been implemented without
@ktsn's help.
License
MIT