immutable-selectors
This package is used for comfort reusable access to parts of deep nested immutable structures. It also allows you to make modification of this structures.
Usage
Example immutable structure
const state = Immutable;
Create selector tree Describe nested selector - object with properties you want to get access:
const selectors = foo: deep:val:{} message:{}
Make selectors from this object:
;;
extendSelecors(selectorsTree, [path]) function recursively converts each key in object to a function(state), which returns corresponding part of immutable structure. At the same time, it saves the hierarchy. It is not returns a new selector - object, but it converts selectors object itself. This allows to use intelliSence in most of IDEs.
Use selector
console; //SomeStringMessageconsole; // Map { "id": 100500, "deep": Map { "val": 500 }, "message": "SomeStringMessage" }console; // SomeStringMessageconsole; // 500
flat option: Each unique key of selectors-tree is dublicated in selectors.flat object:
console; //500console; // SomeStringMessage
root option:
const moduleAData = selectors;console; //100500
Modifications Each selector-function in tree has replace(state, newValue) child function, which returns new immutable structure with changed corresponding part.
var newState = selectorsfoomessage;console; //updated message
var newState = selectorsflatval;console; //new value
var part = selectors;part;var newState = selectorsfoo;console;//fivehundred
Parameters
It is possible to use selectors for collection items access (Lists and Maps). You should specify param property in selector-object at collection level.
Example immutable structure
const state = Immutable;
Create selector tree
const selectors = bar: listItems: param:'x' key:{} caption:{} deep:description:{} ;
Use
console; //secondLIstItem console; //100 console; //someText console;//Map { "key": 200, "caption": "secondLIstItem" } console;//List [ Map { "key": 100, "caption":...
Modifications Use third parameter of replace function to specify collection item.
var newState = selectorsbarlistItemscaption;console; // updated-first
Use with redux
One of use-cases is to replace redux reducers with action-creators, which generates new global state. Example of action-creator:
{ return { ; };}
Root reducer:
{ if actionsetState return actionsetState; else return state; // return combinedReducers(state, action); // You can mix this pattern with classical combineReducers - function. }
Example of using immutable-selectors with redux without reducers.