0.0.5 • Published 6 years ago

compound-util v0.0.5

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

compound

(Available on npm as compound-util)

Like compose, but with rest params also being passed in.

The first param is the return value of the previous function. The rest params remain unchanged, e.g.:

const sum = (x, y) => x + y;
const multiply = (x, y) => x * y;
compound(sum, multiply)(3, 4); // 16

This is particularly useful in redux reducers, for example, when you want to apply multiple modifiers to the state, using the same action object.

const addTodo = (state, action) => ({
    ...state,
    todos: state.todos.concat(action.payload),
  })

const updateTimestamp = (state, action) => ({
    ...state,
    lastUpdated: action.meta.timestamp,
  })

const reducer = (state, action) => {
  switch(action.type) {
    case 'ADD_TODO':
      return compound(addTodo, updateTimestamp)(state, action)
  }
}

Or if you are using redux-actions:

const reducer = handleAction(
  ADD_TODO: compound(addTodo, updateTimestamp)
)
0.0.5

6 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

1.0.0

7 years ago