
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
npm i redux-msgsmall set of functions to help DRY redux code:
import {
// helpers for regular redux
createReducer,
createSelector,
createState,
// special actions called "messages" for advanced code DRYness
createMessage,
createMessagesReducer,
mergeReducers
} from 'redux-msg';
928 bytes in total (gzipped), 0 dependencies, 100% satisfaction
usage examples in this repo
Redux is great but applications written using it tend to attract boilerplate code.
Not much is needed to avoid this: only 3 tiny helper functions for starters, or additional 3 (also tiny) functions if you can handle a little convention.
your redux-aware components should have:
NAME - string a unique name of component. easily changeable when neededMODEL - object the shape of statethat's no magic, just:
export const NAME = 'my awesome unique name'
export const MODEL = { woodoo: true, greeting: 'Howdy', randomNumber: 4 }
this convention is helpful even without any of the helper functions suggested here.
all 6 exported functions are explained below starting from simplest
createReducerconst { createReducer } = require('redux-msg')
if you code reducers with switches or ifs, this function is for you.
const { createReducer } = require('redux-msg');
const reducer = createReducer(MODEL)(reducers)`
where:
MODEL is an object of redux statereducers is an object where:
key is action type (e.g. COUNTER_INCREASE)value is a reducer function of signature (state, action) => state.so instead of this:
const reducer = (state, action) => {
switch(action.type) {
'increase':
return { ...state, count: state.count + 1 }
}
}
you can do this:
const MODEL = { count: 0 }
const reducer = createReducer(MODEL)({
increase: state => ({...state, count: state + 1})
})
createReducer(MODEL)(reducers) returns yet another reducer with
signature (state, action) => state. This means that it can be used
with other redux tools with no problem.
import { createReducer } from 'redux-msg';
export const MODEL = {
count: 0
};
// reducer created with `createReducer`
export const reducer = createReducer(MODEL)({
increase: state => ({ ...state, count: state.count + 1 }),
setCount: (state, action) => ({ ...state, count: action.count })
});
// ... later
dispatch({ type: 'increase' });
// state is now { count: 1 }
dispatch({ type: 'setCount', count: 10 });
// state is now { count: 10 }
createSelectorconst { createSelector } = require('redux-msg')
when you have state, you want to be able to read it easily. easily means from anywhere and always the same way.
let's consider bad approach for a moment.
imagine your store.getState() returns:
{
counterComponent: {
count: 0
}
}
you can create function
const selectCount = state => state.counterComponent.count;
then call it somewhere else
selectCount(store.getState()) // <= 0
however, this doesn't scale well: you need such function for each model
property and it also needs to know full path to reach count.
by following simple convention to name your components, you can
automatically create such select functions with createSelector
without the need to know path to properties.
createSelector(NAME)(MODEL) where:
NAME is a string labeling your component. This should also be part of combineReducers():import counterLogic from 'components/counter/logic';
import todoLogic from 'components/todo/logic';
combineReducers({
[counterLogic.NAME]: counterLogic.reducer,
[todoLogic.NAME]: todoLogic.reducer
});
a
NAMEdefined once for each redux state section is also useful for other helper functions in this library.
MODEL is an object of redux stateobject with keys that are the same as in given MODEL. values are
functions of signature state => any, where state is
store.getState() and any is whatever type that slice of state is.
For example:
logic.js:
const NAME = 'counterComponent';
const MODEL = {
count: 0,
message: 'hello there!'
};
export const select = createSelector(NAME)(MODEL);
assert.deepEqual(Object.keys(selectors), Object.keys(MODEL)) // just to illustrate that both have same keys
console.log(select.count(store.getState())) // <= 0
console.log(select.message(store.getState())) // <= 'hello there!'
this fits really well with react-redux mapStateToProps:
component.js:
import { select } from './logic';
const mapStateToProps = state => ({
count: select.count(state)
});
import { createSelector } from 'redux-msg';
export const NAME = 'counterComponent';
export const MODEL = {
count: 0
};
export const selector = createSelector(NAME)(MODEL);
it can be combined with other selectors easily:
export const selectors = {
...createSelector(NAME)(MODEL),
myOtherSelector: state => state[NAME].specialItem
}
createStateconst { createState } = require('redux-msg')
helper to create a slice of global state for specific component.
can be used as a state "factory", to hydrate createStore when loading
component dynamically or during server side rendering.
can also be used as utility in tests.
const initState = createState(NAME)(MODEL) where:
NAME is a string labeling your component. This should also be part of combineReducers(). See createSelector for more detailsMODEL is an object of redux statea function with signature object -> { [NAME]: { ...MODEL, object } }.
That's pretty much the actual implementation.
createState(NAME)(MODEL) returns function that accepts object and
returns state. The returned state has key name and its value is
shallowly merged MODEL and object.
Code explains better than i do, please see example.
myComponent/redux.js:
import { createState } from 'redux-msg';
const NAME = 'myComponent';
const MODEL = {
default: 'property',
something: 'i am some default value'
};
export const state = createState(NAME)(MODEL);
create-store.js:
createStore from redux accepts second parameter - initial state.
this is where createState may be used
import { createStore, combineReducers } from 'redux';
import myComponent from 'myComponent/redux';
const store = createStore(
combineReducers({
[myComponent.NAME]: myComponent.reducer
}),
{
...createMyComponentState({ something: 'i am NOT default haha!' })
})
after this, store.getState() will return:
{
'myComponent': {
default: 'property'
something: 'i am NOT default haha!'
}
}
FAQs
some functions to organize redux in opinionated way
We found that redux-msg demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.