0.0.7 • Published 7 years ago

graftss-redux-utils v0.0.7

Weekly downloads
-
License
ISC
Repository
-
Last release
7 years ago

redux-utils

a collection of functions I've used at least twice

installation

yarn add graftss-redux-utils

Modules

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.

ParamTypeDescription
propsArray.<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.

ParamTypeDescription
addedMetaObjectThe new pairs to add to a meta value.
objectObjectThe 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.

ParamTypeDescription
getMetafunctionThe function which computes metadata.
creatorfunctionThe 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.

ParamTypeDescription
typeStringThe 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.

ParamTypeDescription
typeStringThe 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.

ParamTypeDescription
typeStringThe type of the resulting action.
fieldsArrayAn 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.

ParamTypeDescription
typeStringThe 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.

ParamTypeDescription
typeStringThe 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.

ParamTypeDescription
treeObjectA tree determining a corresponding tree of creators.
typeAtPathfunctionA 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.

ParamTypeDescription
ffunctionThe action creator mapping.
creatorTreeObjectThe 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.

ParamTypeDescription
initialState*The initial state.
switchObjectObjectThe 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.

ParamTypeDescription
initialState*The initial state of the full map of values.
getKeysfunctionA function that returns the array of keys on which to apply a given action.
valueReducerfunctionThe 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.

ParamTypeDescription
propStringThe 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.

ParamTypeDescription
compareStatesfunctionCompares the computed and expected states.
reducerfunctionThe reducer to be tested.
caseDataObjectContains the actions, initial, and final state.
caseData.initialState*The initial state.
caseData.actionsArrayThe 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

ParamTypeDescription
compareStatesfunctionCompares the computed and expected states.
reducerfunctionThe reducer to be tested.
initialState*The initial state used by the reducer.
caseSeqArray.<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.

ParamTypeDescription
bigObjectThe object that should contain small.
smallObjectThe 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.

ParamTypeDescription
finalObjectThe object that should result from the merge.
initialObjectThe object before the changes.
finalObjectThe changes to merge into the initial object.