js-lenses
Pure, composable, immutable getters and setters for plain JavaScript data structures.
data → (⟩ a → ƒ(a) ⟨) → data
What is it?
Functional lenses are pure functional references into data structures.
Given
var a = b: c: d: 'd' ;
Imperative refereces
/* reference to value { d: 'd' } inside of a */a'b'c; a'b'c; // geta'b'c = 1; // set
Functional references with js-lenses
/* reference to value { d: 'd' } inside of a */var l = L; L; // getL; // set
The difference between these two approaches is that lenses are easily composable and immutable.
Installation
npm i js-lenses
Usage
L.of('propName')
— create lenseL.ofPath('path', 'to', 'value')
— create lense from pathL.get(lense, target)
— get valueL.set(lense, value, target)
— set valueL.update(lense, fn, target)
— update value by applying function to it
; var person = name: 'John' children: boys: name: 'John' name: 'Dirk' ; var nameLense = L; // create lenseL; // extract lense value out of dataL; // set value into data structure var childrenLense = L; // compose lensesvar firstNameChildLense = L; // or create one from path L; // update lense value
Why?
For learning purpose 🎓