kurtsore
A cursor implementation in JavaScript for focusing on atoms with immutable data structures.
Installation
$ npm install kurtsore
Usage
Let's say you are managing some state with an Atom and you have a deeply nested, associative immutable data structure on it. Managing it with a bare atom can get tedious and cursors allow you to focus on a substructure of the atom. The most basic cursor will point to the root of the atom:
var a = k = i = ; var atom = a;var cursor = k; i;//=> true
Focusing on keys and paths
However, cursors can point to keys and paths in the Atom they are constructed from:
var cursorA = k; i;//=> true
Cursors can be derived from others, refining the path they point to:
var cursorC = cursorA; i;//=> true
Atomic operations
Cursor instances support Atom's deref
method as you have seen in the above examples, but
they also implement swap
so you can apply a function to the focused structure of the
original atom:
var cursorB = k; cursorB; i//=> truei;//=> true
If you instead want to replace the focused structure, you can use reset
:
atom; var cursorD1 = k;cursorD1;//=> 1 cursorD1;i;//=> true
Snapshots
Cursors remember a snapshot of the focused structure when they were created, this helps
libraries like react-kurtsore implement efficient
shouldComponentUpdate
in React components.
atom; var cursorB = k;i;//=> true cursorB;var newCursorB = cursorB;i;//=> true
License
BSD 2-clause license, Copyright 2015 Alejandro Gómez.