npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-candy

v2.0.0

Published

Generate redux updeep based reducer and actions.

Downloads

4

Readme

🍭 redux-candy

IMPORTANT: This project maybe useful for development or toy but maybe not for produciton.

Generate redux updeep based reducer and actions.

npm Travis

Installation

$ npm install redux-candy
$ yarn add redux-candy

Example

const initialState = {
  counter: 0
}
const reducer = createReducer(initialState)

// counter
const increment = createAction('INCREMENT', 'counter', () => ( (i) => i + 1 ))
const decrement = createAction('DECREMENT', 'counter', () => ( (i) => i - 1 ))

const Counter = ({dispatch, counter}) => {
  return (
    <div>
      <div>{counter}</div>
      <button onClick={() => dispatch(increment())}>increment</button>
      <button onClick={() => dispatch(decrement())}>decrement</button>
    </div>
  )
}

const CounterContainer = connect(state => state)(Counter)
const store = createStore(reducer)
const App = () => (
  <Provider store={store}>
    <CounterContainer />
  </Provider>
)

You can show real demo and example source

Usage

redux-candy provide very stupid reducer that apply passed action.

const initialState = {
  counter: 0
}
const reducer = createReducer(initialState)

And you must pass action that has update function

// counter
const increment = createAction('INCREMENT', 'counter', (i) => i + 1 )
const decrement = createAction('DECREMENT', 'counter', (i) => i - 1 )

You can pass plain action like this.

const increment = () => {
  type: 'INCREMENT',
  payload: {
    // [key]: updateFunction
    counter: () => ( (i) => i + 1 )
  }
}

If you want replace value

const replaceValueAction = createAction('INCREMENT', 'someValue')
// or
const replaceValueAction = (value) => {
  type: 'INCREMENT',
  payload: {
    // [key]: updateFunction
    someValue: value
  }
}

You can mutate oldValue

const listItemAppendAction = createAction('INCREMENT', 'someList', (oldList, value) => [...oldList, value]))
// or
const listItemAppendAction = (value) => {
  type: 'INCREMENT',
  payload: {
    // [key]: updateFunction
    someList: (oldList) => [...oldList, value]
  }
}

You can pass nested property.

const actionCreator = createAction('SOME_TYPE', ['a', 'b', 'c'])
const action = actionCreator('value')
// or
const action = {
  type: 'SOME_TYPE',
  payload: {
    a: {
      b: {
        c: 'value'
      }
    }
  }
})

You can pass key-value action.

const actionCreator = createAction('SOME_TYPE', (userName, value) => {
  return {
    user: {
      [userName]: value
    }
  }
})
const action = actionCreator('bob', 100)

API

Reducer

createReducer(initialState: Object, [actionFilter: Function])

Generate updeep based rootReducer. This reducer accept all action and return updeep(action.payload, state).

This reducer ignore below action too.

  • Not flux standard action.
  • Non object type payload action.
    • Ignore action example: { type:"TYPE", payload: "someValue" }

If you want controll update condition, pass actionFilter

const updateCondition = ( action ) => {
  // update when pass SOME_ACTION
  return action.type === "SOME_ACTION"
}

Action

Helper for create action for redux-candy reducer. This actionCreator return FSA compatible action.

createAction(actionType: String, targetProperty: String, [updateFunction: Function], [metaCreator: Function])

Generate update targetProperty value action.

const actionCreator = createAction('ACTION', 'bob', (state, input) => state + input)

// const initialState = { bob: 1 }
store.dispatch(actionCreator(10)))
// state:
// {
//   bob: 11 // 1 + 10
// }

updateFunction get arguments (baseValue, ...inputs).

All action arguments pass to updateFunction. You can use that.

const actionCreator = createAction('ACTION', 'bob', (state, input1, input2) => state + input1 + input2)

actionCreator(10, 20)

createAction(actionType: String, targetProperties: Array, [updateFunction: Function], [metaCreator: Function])

Generate update targetProperty value action.

// const initialState = { users: { bob: 1 } }
const complexActionCreator = createAction('ACTION', ['users', 'bob'], (state, val) => state + val)
store.dispatch(complexActionCreator(10)))
// state:
// {
//   users: {
//     bob: 11
//   }
// }

createAction(actionType: String, updateFunction: Function, [metaCreator: Function])

Proxy redux-action

You can fully control payload.

// const initialState = { bob: 1 }
const complexActionCreator = createAction('SOME_COMPLEX', (value, key) => {
  return {
    [key]: (i = 0) => (i + value)
  }
})
store.dispatch(complexActionCreator(10, 'bob')))
// state:
// {
//   bob: 11
// }

store.dispatch(complexActionCreator(10, 'bob')))
// state:
// {
//   bob: 1,
//   sam: 10
// }