graftss-redux-utils

0.0.7 • Public • Published

redux-utils

a collection of functions I've used at least twice

installation

yarn add graftss-redux-utils

Modules

action
reducer
testing

action

action~keyMirror ⇒ Object

Given an array of strings, creates an object whose keys and corresponding values are those strings.

Kind: inner property of action
Returns: Object - Returns an object with matching keys and values.

Param Type Description
props Array.<String> An array of properties.

action~addMeta ⇒ Object

Adds to the meta property of an object.

Kind: inner property of action
Returns: Object - Returns the updated object.

Param Type Description
addedMeta Object The new pairs to add to a meta value.
object Object The object which will receive addedMeta.

action~withMeta ⇒ function

Adds metadata to the result of an action creator using the return value of getMeta, which receives the same arguments as the creator.

Kind: inner property of action
Returns: function - Returns a new action creator.

Param Type Description
getMeta function The function which computes metadata.
creator function The original action creator.

action~constantCreator ⇒ function

Generates an action creator with no payload.

Kind: inner property of action
Returns: function - Returns a constant action creator.

Param Type Description
type String The type of the resulting action.

action~identityCreator ⇒ function

Generates an action creator whose payload is its only argument.

Kind: inner property of action
Returns: function - Returns an identity action creator.

Param Type Description
type String The type of the resulting action.

action~argListCreator ⇒ function

Generates an action creator whose payload is an object with keys corresponding to the string values in fields, and values corresponding to the action creator's arguments.

Kind: inner property of action
Returns: function - Returns an action creator.

Param Type Description
type String The type of the resulting action.
fields Array An array of fields to include in the payload.

action~errorCreator ⇒ function

Generates an action creator of the given type, which has only an error field.

Kind: inner property of action
Returns: function - Returns an action creator.

Param Type Description
type String The type of the resulting action.

action~generateCreator ⇒ function

Generates a creator with given type, payload, and meta values.

If getPayload or getMeta is a function, it will be called with the same arguments as the creator, and its returned value will be used as the payload or meta as appropriate.

Kind: inner property of action
Returns: function - Returns an action creator.

Param Type Description
type String The type of the resulting action.
getPayload * The payload of the resulting action.
getMeta * The metadata of the resulting action.

action~generateCreatorTree ⇒ Object

Transforms an input tree into a tree of action creators.

A function or array value in the input tree corresponds to an action creator, which is treated as a leaf in the tree. An object value corresponds to a nontrivial subtree, which is generated recursively.

The path to an action creator in the input tree corresponds to both the action creator's resulting type, and to the creator's path in the output tree. For example, given the input tree: { PATH: { TO: { TREE: creator } } }, in the output tree, creator would create actions of type "PATH/TO/TREE", and would be located at output.path.to.tree.

The action type can be customized via the second argument typeAtPath, which is a function that receives the path from the root object to the child being processed, as an array of property strings, and returns the action type. The default typeAtPath is path => path.join('/').

If a leaf in the input tree is a function, the corresponding action creator will pass its arguments into that function to get its payload. If the leaf is an array, the first and second elements in the array will be used to generate the payload and meta values of the action, respectively.

Kind: inner property of action
Returns: Object - Returns a tree of action creators.

Param Type Description
tree Object A tree determining a corresponding tree of creators.
typeAtPath function A function mapping a path to an action type.

action~mapCreatorTree ⇒ Object

Applies a function to each action creator node in a creator tree.

Kind: inner property of action
Returns: Object - Returns a mapped creator tree.

Param Type Description
f function The action creator mapping.
creatorTree Object The tree to map over.

reducer

reducer~switchReducer ⇒ function

Creates a reducer emulating a switch statement on the action type, using an object whose keys are the action types and whose values are reducers corresponding to each action type.

Kind: inner property of reducer
Returns: function - Returns a reducer generated from the input.

Param Type Description
initialState * The initial state.
switchObject Object The object containing the switch data.

reducer~mapReducer ⇒ function

Generates a single reducer that acts on a map whose values are of the same shape, and can be acted on by the same reducer.

The getKeys argument receives the same arguments as the reducer itself, i.e. (state, action).

Kind: inner property of reducer
Returns: function - Returns a reducer acting on the map as a whole.

Param Type Description
initialState * The initial state of the full map of values.
getKeys function A function that returns the array of keys on which to apply a given action.
valueReducer function The reducer acting on each value.

reducer~fromPayload ⇒ function

Creates a function that reads a given property from a payload, and that has the same signature as a reducer (i.e. the action is assumed to be the second argument).

Kind: inner property of reducer
Returns: function - Returns a function mapping an action to the payload prop.

Param Type Description
prop String The property to read from the action payload.

testing

testing~reducerCaseTest ⇒ *

Test the resulting state from a reducer after applying successive actions.

Kind: inner property of testing
Returns: * - Returns the final computed state.

Param Type Description
compareStates function Compares the computed and expected states.
reducer function The reducer to be tested.
caseData Object Contains the actions, initial, and final state.
caseData.initialState * The initial state.
caseData.actions Array The actions to apply.
caseData.finalState * The expected final state.

testing~reducerSeqTest

Tests a reducer by repeatedly running actions against an initial state, then verifying the computed state is as expected.

The argument caseSeq should be an array of objects with actions and finalState properties. caseSeq.actions should be an array of actions given to the reducer in sequence, with the resulting state being compared to caseSeq.finalState.

Kind: inner property of testing

Param Type Description
compareStates function Compares the computed and expected states.
reducer function The reducer to be tested.
initialState * The initial state used by the reducer.
caseSeq Array.<TestData> An array of objects containing test data.

testing~contains ⇒ Boolean

Tests if an object big deeply contains an object small, i.e. for all primitive value found in small, the same value is found at the same path in big.

Kind: inner property of testing
Returns: Boolean - Returns true if big contains small.

Param Type Description
big Object The object that should contain small.
small Object The object that should be contained in big.

testing~differs ⇒ Boolean

Tests if an object final is the result of deeply merging the two objects initial and changes.

Kind: inner property of testing
Returns: Boolean - Returns true if initial changes into final.

Param Type Description
final Object The object that should result from the merge.
initial Object The object before the changes.
final Object The changes to merge into the initial object.

Readme

Keywords

none

Package Sidebar

Install

npm i graftss-redux-utils

Weekly Downloads

0

Version

0.0.7

License

ISC

Last publish

Collaborators

  • graftss