Skip to content
/ compound Public

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

Notifications You must be signed in to change notification settings

pdme/compound

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
)

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages