Skip to content

Commit

Permalink
self review
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumecrespel committed Aug 2, 2017
1 parent e1418d8 commit 528f0c7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ todos.get('1')(state)
|---|---|---|
|`mapAction(<mapper(action)>)`| create middleware and map only redux action | mapper(action) is mandatory |
|`mapState(<regex>)(<mapper(state)>)`| create middleware and map the state of the corresponding redux actions type by the regex |
|`reducer(<yourReducer(action, state)>)`| create middleware and map action and state |
|`reducer(<yourReducer(action, state)>)`| create middleware and map state depending on the action |
|`mapPayload(<regex>)(<mapper(payload)>)`| create middleware and map the payload of the corresponding redux actions type by the regex |

#### Example, we create a middleware but we modify only the action :
Expand Down Expand Up @@ -232,12 +232,10 @@ import factory from 'trampss-redux-factory'
// import your helpers
import { reducer } from 'trampss-redux-factory/helpers'

// define a function to map action and state
const mapper = (action, state) =>
({
action: { ...action, type: `SET_${action.type}` },
state: { ...state, todos: 'TODO_CHANGED' }
})
// define a function to map state depending on the action
const mapper = (action, state) => {
if (/SET_TODOS/.test(action.type)) return { todos: 'TODOS_CHANGED' }
return state
// create your reducer and transform the action and state before core middleware
export default factory({ pre: [reducer(mapper)] })({ key: 'id', path: 'api.raw', name: 'todos' })
```
Expand Down
2 changes: 1 addition & 1 deletion src/__snapshots__/helpers.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Object {
exports[`helpers/reducer should add reducer to change state 1`] = `
Object {
"action": Object {
"payload": "change",
"payload": "payload",
"type": "CUSTOM",
},
"state": "CUSTOM_STATE",
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const reducer = mapper =>
// middleware signature
() => () => () => ctx => mapper(ctx.action, ctx.state)
() => () => () => ctx => ({ ...ctx, state: mapper(ctx.action, ctx.state) })


export const mapAction = mapper =>
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const customCtx = {
const mapperAction = () => 'ACTION MAPPED !'
const mapperPayload = () => 'PAYLOAD MAPPED !'
const mapperState = state => `${state} MAPPED !`
const customReducer = (action, state) => (/CUSTOM/.test(action.type) ? { action: { ...action, payload: 'change' }, state: 'CUSTOM_STATE' } : { action, state })
const customReducer = (action, state) => (/CUSTOM/.test(action.type) ? 'CUSTOM_STATE' : state)

const execHelper = context => helper => helper()()()(context)

Expand Down

0 comments on commit 528f0c7

Please sign in to comment.