lorgnette
A set of lenses
Lenses are helper objects to retrieve or update data in immutable collections.
Basics
Lens is an object which allows to access object properties. Any lens has to
implement next three methods:
get(obj)
set(obj, value)
update(obj, callback)
Methods get
and update
return new object instead of modifying given one.
Maybe
Method get
returns a value wrapped in Maybe monad. Maybe instance has
following methods:
isJust()
isNothing()
getOr(orElse)
then(func)
recover(func)
Some examples:
import {nothing, just} from 'lorgnette';
just('value').getOr('anotherValue')
nothing.getOr('anotherValue')
function appendBang(s) {
return just(s + '!');
}
nothing.then(appendBang)
just('value').then(appendBang)
just('value').then(() => nothing))
nothing.recover(() => 42)
just('value').recover(() => 42)
Predefined lenses
prop
Lens prop
allows access to object properties by name.
import { lens } from 'lorgnette';
let age = lens.prop('age');
age.get({name: 'John'})
age.get({name: 'John', age: 42})
age.set({name: 'John', age: 42}, 24)
age.update({name: 'John', age: 42}, x => x + 1)
This lens can also be configured to return default value instead of Nothing
when property does not exist.
import { lens } from 'lorgnette';
let age = lens.prop('age', 18);
age.get({name: 'John'})
age.get({name: 'John', age: 42})
at
Lens at
allows access to array elements by given index.
import { lens } from 'lorgnette';
let second = lens.at(1);
second.get([])
second.get([1, 2, 3, 4, 5])
second.set([1, 2, 3, 4, 5], 7)
second.update([1, 2, 3, 4, 5], x => x + 7)
let last = lens.at(-1);
last.get([1, 2, 3, 4, 5])
last.set([1, 2, 3, 4, 5], 7)
first
Lens first
is similar to at(0)
but it prepends new value when set
or
update
is called.
import { lens } from 'lorgnette';
let first = lens.first();
first.get([])
first.get([1, 2, 3, 4, 5])
first.set([1, 2, 3, 4, 5], 7)
first.update([1, 2, 3, 4, 5], x => '' + x)
last
Lens last
is similar to at(-1)
but it appends new value when set
or
update
is called.
import { lens } from 'lorgnette';
let last = lens.last();
last.get([])
last.get([1, 2, 3, 4, 5])
last.set([1, 2, 3, 4, 5], 7)
last.update([1, 2, 3, 4, 5], x => '' + x)
firstOf/lastOf
Lenses firstOf
/ lastOf
allow access to array elements by predicate.
firstOf' looks for an element satisfying a predicate from the beginning of an array.
lastOf` does the same but searched backward.
import { lens } from 'lorgnette';
let second = lens.firstOf(obj => obj.id === 2);
second.get([])
second.get([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}])
second.set([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}], 7)
second.update([{id: 1}, {id: 2, x: 4}, {id: 3}, {id: 4}, {id: 5}], ({x}) => x + 7)
Chaining
Lenses can be chained so access to nested properties is possible.
import { lens } from 'lorgnette';
let lastCartItem = lens.prop('items', []).last();
let cart = {
items: [
'potato',
'cheese'
]
};
lastCartItem.get(cart)
lastCartItem.set(cart, 'carrot')
Multilens
Sometimes it is usefull to access multiple properties at once. Multilens allows
to do this.
import { lens, multi } from 'lorgnette';
let lastCartItem = lens.prop('items', []).last();
let totalCount = lens.prop('total');
let cartLens = multi(lastCartItem, totalCount);
let cart = {
total: 2,
items: [
'potato',
'cheese'
]
};
cartLens.get(cart)
cartLens.set(cart, 'carrot', 3)
cartLens.update(cart, () => 'carrot', x => x + 1)