Slice and compose redux-type reducers.
Herbert Vojčík a17f719b4b cow: Export. | 7 年 前 | |
---|---|---|
lib | 7 年 前 | |
src | 7 年 前 | |
.gitignore | 7 年 前 | |
LICENSE | 7 年 前 | |
README.md | 7 年 前 | |
package.json | 7 年 前 |
Slice and compose redux-type reducers.
subReducer(key, reducer, additionalKey, ...)
Creates a wrapper reducer that call reducer
on the substate specified by key
.
You may use dot notation.
Rest of the state is left untouched.
const r = subReducer("persons", personReducer);
r({persons: ["John", "Jill"], cars: ["Honda"]}, cityon);
// => {
// persons: personReducer(["John", "Jill"], action),
// cars: ["Honda"]
// }
Respects redux convention that if no change was made,
the identical object should be returned. So in previous case,
if personReducer
would return the identical array,
r
would return the state object it was passed in.
If persons were deeper in hierarchy, it could have been created as
const r = subReducer("files.persons", personReducer);
for example.
You may pass additional keys (also with possible dot-notation) as addition arguments. In that case, additional parts of state will be fetched and passed to a sub-reducer:
const r = subReducer("persons", personReducer, "assets.cars");
r({persons: ["John", "Jill"], assets: {cars: ["Honda"]}}, action);
// => {
// persons: personReducer(["John", "Jill"], action, ["Honda"]),
// assets: {cars: ["Honda"]}
// }
This technique is mentioned in Redux docs, in "Beyond combineReducers" page.
composeReducers(reducer1, reducer2, ...)
Creates a wrapper reducer that calls passed reducers one after another, passing intermediate states. and returning the result of the last one.
Useful to "concatenate" a few subReducer
s. like:
composeReducers(
subReducer("files.persons", personReducer, "assets.swag"),
subReducer("files.clients", clientReducer, "news"),
subReducer("assets", assetReducer),
baseReducer
)
cowValueModel(key)
Creates an object with set of functions allowing
to set
or get
specified key from any object.
You can use dot notation.
Setting return a copy with specified (sub-)property changed; in case no change actually happens, returns the original object.
const name = cowValueModel("name");
name.get({name: "Tom"});
// => "Tom"
name.set({name: "Tom"}, "Jerry");
// => {name: "Jerry"}
const city = cowValueModel("address.city");
const object = {address: {city: "New York"}};
city.get(object);
// => "New York"
city.set(object, "London");
// => {address: {city: "London"}}
object;
// => {address: {city: "New York"}}
city.set(object, "New York") === object;
// => true