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

dva-reducer

v1.1.3

Published

dva reducer helper, redux reducer helper, extend the target prop inside the tree

Downloads

14

Readme

dvaReducer

features

  1. this is mainly used to be a helper for the dva's reducer
  2. also can be treated as a fast copy tool without deep walking into the json tree
  3. of course, redux reducer can also be applied with dvaReducer
  4. support default value as the placeholder
  5. call dvaReducer with no arg can be a useful method to cancel the ctrl+c|ctrl+v work

install package

npm install dva-reducer --save

tests

git clone https://github.com/peterlevel1/dva-reducer.git someDir/dva-reducer
cd someDir/dva-reducer
npm install
npm run test

purpose

to help your dva model reducer codes to be more readable
// file: some dir/app.js
var dvaReducer = require("dva-reducer")
var dva = require("dva")

dva.model({
  namespace: "caculator",
  state: {
    num: {
      num1: {
        num2: {
          num3: {
            num4: 0
          }
        }
      }
    }
  },
  reducers: {
    // the traditional reducer to update a value:
    // if the nested relation is a little deep,
    // we see the result of code is horrible,
    // and many bugs may show here, to avoid that you must be careful about the pyramid codes,
    // or you would meet some unknown bug
    "add": function(state, action) {
      return {
        ...state,
        num: {
          ...state.num,
          num1: {
            ...state.num.num1,
            num2: {
              ...state.num.num1.num2,
              num3: {
                ...state.num.num1.num2.num3,
                num4: state.num.num1.num2.num3.num4 - action.payload
              }
            }
          }
        }
      }
    },
    // we see the graceful codes with dvaReducer here
    "minus": dvaReducer({
      props: "num.num1.num2.num3.num4",
      update: function (state, payload, value) {
        return value - payload
      }
    })
  }
})

dva.route(...)
dva.start(...)

module.exports = dva

// file: some component or some route
dispatch({
  type: "caculator/add",
  payload: 10
})

dispatch({
  type: "caculator/minus",
  payload: 3
})

require("some dir/test.js")

// some dir/test.js
var dva = require("some dir/app.js")
var assert = require("assert")

var num = dva._getState().caculator.num
assert.equal(num, 7, "caculator.num: " + num)
// true

dvaReducer

  • dvaReducer: a factory to make the dva reducer
    • @param {Object|Null} opts if no arg, the reducer would be an save reducer, the reducer would receive(state, action), action.payload must be provided to override the state's some props directly, say: dispatch({type: "some namespace/save", payload: { num: 1 }}), and the state is: { num: 0 }, so the state would be { num: 1 }

      • @param {Function} update to update the payload, the update take 3 arg: (state, payload, value), state: the state received by the reducer, payload: the action.payload, received by the reducer, so payload prop should exist on the action, value: the src value for the target prop

      • @param {Array|String} props the last one of the props is the target prop to be updated, "dva.is.awesome", "dva is awesome", ["dva", "is", "awesome"] would all be acceptable, so the target prop is awesome ready to be updated when reducer called by the dispatch method of the redux store

      • @param {*|Function} defaultValue when payload not provided, defaultValue would be the placeholder if defaultValue is function, it should return the value for the target prop, defaultValue is useful when you want a value to be a constant, say: hide: active: false(defaultValue), show: active: true(defaultValue)

    • @return {Function} reducer after the configurations, the reducer is just what we really want

the returned function: reducer

  • reducer(state, action)
    • @param {Object|Array} state object is advised for the type of the state

    • @param {Object} action the action type must be resolved by the upper layer logic

      • @param {Boolean} partial default value is true, set partial false if ensure to override the prop value, when payload and the action are both object

      • @param {*} payload the new value for the target prop(the last prop)

    • @return {Object} an object with required route line props updated

call dvaReducer.prod() if online

this would close the logs if sth wrong when dvaReducer valid your arg or prop value, so my advice is to see the test of dvaReducer first, and read the param doc up there, what's more, make some tests to keep everything in your control

reducer and shallow extend

what the reducer return is an object with the given props route updated

var obj = {x: 1}
var state = {
  a: obj,
  b: {
    c: {
      d: 100
    }
  }
}

var action = {
  payload: 5
}

var reducer = dvaReducer({
  props: "b.c.d"
})

var nextState = reducer(state, action)

// nextState.a is equal to state.a, as the "a" prop is not within b.c.d
// but nextState.b or nextState.b.c or nextState.b.c.d is not equal to the state's according value