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

v0.2.6

Published

write less code with redux

Downloads

14

Readme

redux-less

write a redux reducer with less code, inspired by mirror

install

npm install redux-less

example online

import { getReducer, reduxLessMiddleware } from 'redux-less';
import { applyMiddleware, createStore } from 'redux'
const store = createStore(
  rootReducer,
  initialState,
  // you must apply "reduxLessMiddleware" to your store
  applyMiddleware(reduxLessMiddleware, ...otherMiddlewares),
)
// ...
// you can use "store.replaceReducer" and "combineReducers" to apply this reducer
const reducer = getReducer({
  key: 'todo',
  initialState: [],
  getTodoList(dispatch, getState, args) { // async reducer can not omit "args" param
    fetch('./api/todos')
      .then(res => res.json())
      .then(list => {
        dispatch(getState().concat(list)) // dispatch the new state
      })
  },
  addTodo(state, action) { // pure function as sync reducer, owns two params
    return state.concat(action.payload) // return the new state
  },
  removeTodo(state, action) {
    const todos = state.slice()
    todos.splice(action.payload, 1)
    return todos
  },
})

// for react-redux, binding to container
export default connect(state => ({
  todoList: state[reducer.key], // reducer.key == "todo"
}), () => reducer.actions)(props => { // reducer.actions is an actionCreators map
  return (
    <div>
      {/* ... */}
      <button onClick={() => props.addTodo('new item')}>add</button>
    </div>
  )
})

API

import { 
  getReducer, 
  reduxLessMiddleware, 
  getActionType, 
  reduxLessMiddlewareWithListener 
} from 'redux-less'
// or using an umd version with global namespace: `ReduxLess`
const getReducer = ReduxLess.getReducer
  • getReducer(model: object): function

    get the reducer function, you can see the example above, it is quite clear
    the model contains key as the reducer key and initialState as the state[key] initial value
    other properties in model must be reducer function

  • reduxLessMiddleware

    you must apply "reduxLessMiddleware" to your store

  • reduxLessMiddlewareWithListener(listener: function)

    if you want to listen any action, you can call this api
    listener is a function with the action as param
    if listener returns false, then the action will not be dispatched

  applyMiddleware(reduxLessMiddlewareWithListener(action => {
    // action is Flux Standard Action, https://github.com/acdlite/flux-standard-action
  }))
  • getActionType(reducerKey: string, reducerName: string): string

    get the action.type by reducer key and reducer function name

  reduxLessMiddlewareWithListener(action => {
    if (getActionType('todo', 'addItem') === action.type) {
      if (!action.payload) return false
    }
  })

license

MIT