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

@evoja/redux-reducers

v0.0.4

Published

Helper functions of creating reducers

Downloads

692

Readme

redux-reducers npm version Build Status

createComplexEvReducer

The function creates a reducer consisted of subreducers. The ubreducer is called only when type of the action matches to declaration. It extracts substate of the state and passes the subobject to the subreducer.

Parameters

createComplexEvReducer(default_state, array_of_declarations)

Every declaration in array is an array: [path_to_substate, types, subreducer]

path_to_substate. String of zero or more parts separated by periods: 'part1.part2.part3.

  • If the part is an alphanumberic string it's just a key of subobject in the state.
  • If the part is wrapped by braces {value.from.action} then it's a parameter. Value of parameter comes from the action.

types. String or array of strings. Set of action types when subreducer must be called.

subreducer. Function(substate, action, fullstate).

Examples

var defaultState = {m: 5, c: [1, 0]}
var reducer = createComplexEvReducer(defaultState, [
  ['m', 'INC_M', x => x + 1],
  ['c.{payload.a}', ['INC_C', 'INC_M'], x => x + 1],
  ['c.{payload.a}', 'M_PLUS_C', (x, _, state) => x + state.m],
])

reducer(undefined, {type: 'SOME_TYPE'})
// => {
//   m: 5,
//   c: [1, 0]
// }

reducer(undefined, {type: 'INC_M', payload: {a: 0}})
// => {
//   m: 6,
//   c: [2, 0]
// }

reducer({m: 100, c: [10, 20]}, {type: 'INC_C', payload: {a: 1}})
// => {
//   m: 100,
//   c: [10, 21]
// }

reducer({m: 100, c: [10, 20]}, {type: 'M_PLUS_C', payload: {a: 1}})
// => {
//   m: 100,
//   c: [10, 120]
// }

wrapEvReducer

Wraps reducer processing substate into reducer processing parent state.

Parameters

wrapEvReducer(path_to_substate, ev_reducer)

path_to_substate. String of zero or more parts separated by periods: 'part1.part2.part3. Parts must be alphanumeric strings.

ev_reducer. Function(substate, action). Reducer must be ev_reducer created by the createComplexEvReducer function or already wrapped by wrapEvReducer.

Examples

var defaultState = {m: 5}
var subreducer = createComplexEvReducer(defaultState, [
  ['m', 'INC_M', x => x + 1],
  ['m', 'A_PLUS_M', (x, _, state) => x + state.a],
])

var reducer = wrapEvReducer('sub.st', subreducer)

reducer(undefined, {type: 'SOME_TYPE'})
// => {sub: {st: {m: 5}}}

reducer(undefined, {type: 'INC_M'})
// => {sub: {st: {m: 6}}}

reducer({sub: {st: {m: 100}}}, {type: 'INC_M'})
// => {sub: {st: {m: 101}}}

reducer({sub: {st: {m: 100}}, a: 10}, {type: 'A_PLUS_M'})
// => {sub: {st: {m: 110}}}

chainReducers

chainReducers(array_of_reducers) Takes an array of reducers and returns a combination of every reducer in the array. If every reducer in the array is an ev_reducer it return the ev_reducer. That means it call only necessary subreducers matching type of the action.

Examples

var r1 = createComplexEvReducer({m: 5, k: 10}, [
  ['m', ['INC_M', 'M_PLUS_N'], x => x + 1],
])
var r2 = createComplexEvReducer({n: 50, k: 20}, [
  ['n', ['INC_M', 'DEC_N'], x => x - 1],
])
var r3 = createComplexEvReducer({n: 50, k: 20}, [
  ['n', 'M_PLUS_N', (x, _, state) => state.m + x],
])

var reducer = chainReducers([r1, r2, r3])

reducer(undefined, {type: 'SOME_TYPE'})
// => {m: 5, n: 50, k: 10}

reducer(undefined, {type: 'INC_M'})
// => {m: 6, n: 49, k: 10}

reducer({m: 5, n: 10}, {type: 'INC_M'})
// => {m: 6, n: 9}

reducer({m: 5, n: 10}, {type: 'DEC_M'})
// => {m: 5, n: 9}

reducer({m: 5, n: 10}, {type: 'M_PLUS_N'})
// => {m: 6, n: 16}