Slice and compose redux-type reducers.
Herbert Vojčík 835dd48147 README: Fix. | пре 7 година | |
---|---|---|
lib | пре 7 година | |
src | пре 7 година | |
.gitignore | пре 7 година | |
LICENSE | пре 7 година | |
README.md | пре 7 година | |
package.json | пре 7 година |
subReducer(key, reducer, additionalKey, ...)
Creates a wrapper reducer that calls reducer
on the sub-state 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"]}, action);
// => {
// 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 overloaded function allowing
to set or get specified key from any object.
Get when one arg, set when two args.
Specify keys by passing a list of keys to cowValueModel
.
You can use dot notation.
Set-usage (two-arg) returns a copy with specified (sub-)property changed; in case no change actually happens, returns the original object.
const name = cowValueModel("name");
name({name: "Tom"});
// => "Tom"
name({name: "Tom"}, "Jerry");
// => {name: "Jerry"}
const city = cowValueModel("address.city");
const city2 = cowValueModel("address", "city");
const object = {address: {city: "New York"}};
city(object);
// => "New York"
city2(object);
// => "New York"
city(object, "London");
// => {address: {city: "London"}}
city2(object, "London");
// => {address: {city: "London"}}
object;
// => {address: {city: "New York"}}
city(object, "New York") === object;
// => true
city2(object, "New York") === object;
// => true
If you put a number in a list of keys to use,
an object will be treated as an array (unlike the default string case,
where it is treated as an object), so copy wil be created
using [...obj]
, not using {...obj}
.
That way you can create eg. const c = cowValueObject("person", 34, "name")
to access obj.person[34].name
with c(obj)
/ c(obj, val)
.
typedAction(type, fn)
This creates an action creator function that amends existing fn
by:
type
property to the result, as well asTYPE
property to action creator itself.So for example:
export const answerQuestion =
typedAction("answer question", (index, answer) => ({
payload: {index, answer}
}));
allows you to call answerQuestion(2, "Albert Einstein")
to create
{type: "answer question", payload: {index: 2, answer: "Albert Einstein"}}
;
but it also allows you to use case answerQuestion.TYPE:
in your reducer,
since answerQuestion.TYPE === "answer question"
.
IOW, this removes the two-space problem of having const FOO_BAR_TYPE
as well as fooBar
action creator.