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

v4.0.5

Published

a set of helpers / creators for redux

Downloads

65

Readme

Purpose

Redux-friends is a set of helpers designed to remove at its best the verbose part of redux use. It is therefore opiniated on the declaration part, by trying to let you declare only what you can't avoid.

This package is designed to work with reduxifire which I maintain too, a CLi to generate a redux folder based on this library's helpers.

See below the example section for a redux implementation using redux-friends

Installation

npm install redux-friends

or

yarn add redux-friends

Types

In constants.js

import { createTypes } from 'redux-friends';

export const REDUCER_KEY = '<some key like forms>'
// used in reducer , see below
export const LOADED = 'loaded';
export const DATA = 'data';

const MyTypesArray = [SET_LOADED, SET_DATA];

export const TYPES = createTypes(REDUCER_KEY, MyTypesArray);

return an object of actions in the form

{ [ACTION_NAME]: `${REDUCER_KEY}/${ACTION_NAME}`}

to target an action (in the reducer part), or declare an action function ( through the createAction helpers), we can declare

TYPES[ACTION_NAME]

( see action/reducer example below )

createTypes rests on redux-types, which I collabored on, and is included here to keep harmony in the package API

Actions

in actions.js

import { createAction, createActionWithMeta } from 'redux-friends';
import { SET_LOADED, SET_DATA, TYPES, LOADED, DATA} from './constants';
// will return a function which accepts a payload argument only
export const setLoaded = createAction(TYPES[SET_LOADED]);
// will return a function which accepts payload and meta arguments
export const setLoadedWithMeta = createActionWithMeta(TYPES[SET_LOADED]);

setLoaded will return :

payload => ({
	payload: payload,
	type: '<injected type with createAction>'
});

setLoadedWithMeta will return :

 (payload, meta ) => ({
	payload: payload,
	meta: meta,
	type: '<injected type with createActionWithMeta>'
});

mapStateToProps

const mapStateToProps = createMapStateToProps({
	formValues: getFormValues,
	loginError: getLoginError,
});

is equivalent to

const mapStateToProps = state => ({
	formValues: getFormValues(state),
	loginError: getLoginError(state),
});

mapDispatchToProps

export const mapDispatchToProps = createMapDispatchToProps({ onLogin: login });

is equivalent to

export const mapDispatchToProps = dispatch => bindActionCreators({ onLogin: login }, dispatch);

Reducer

import { createReducer } from 'redux-friends';
import { SET_LOADED, SET_DATA, TYPES, LOADED, DATA } from './constants';

const defaultState = {};

const behaviors = {
	[TYPES[SET_LOADED]]: (state, { payload }) => ({ ...state, [LOADED]: payload }),
	[TYPES[SET_DATA]]: (state, { payload }) => ({ ...state, [DATA]: payload }),
	// variant if action is created with createActionWithMeta
	[TYPES[SET_LOADED]]: (state, { payload, meta }) => ({ ...state, [LOADED]: payload, someOtherKey:meta }),

};

export default createReducer(behaviors, defaultState);

Store

There are two createStore exposed in this package. Both embed redux-devtools and redux-thunk middleware.

The first : has only features described above.

import { createStore } from 'redux-friends';


const middlewares = ['whatever middlewares you want'];

export const store = createStore(rootReducer, ?middlewares );

( note that middleware argument is optional argument )

The second one embed redux-persist. Redux-persist being treated as external dependency in the lib, you'll have to install it on your project.

import createStore from 'redux-friends/build/createPersistedStore.js';


const middlewares = ['whatever middlewares you want'];

export const { store, persistor } = createStore(rootReducer, ?middlewares );

( note that middleware argument is optional argument )

:warning: In this case, createStore is the default export, so use default import ;)

( the reason being that since this file relies on special dependencies that the rest of the lib does not require, it is treated as a separate bundling process, hence the import path )

Reducer Helpers

Since reducers are often designed to manage the same data structures, here is a list of helpers to minimize the declaration part to achieve same goals.

I mostly design atomic reducers, where state is the value of the reducer key. ( yes that implies doing a massive use of combineReducers ^^, which is a good design in my opinion )

( equivalences in reducer behaviours)

addPayloadToState

usefull to add an item in a collection for example

const behaviors = {
	[TYPES[ADD_ITEM]]: addPayloadToState,
};

is equivalent to :

const behaviors = {
	[TYPES[ADD_ITEM]]: (state, { payload }) => [...state, payload],
};

addPayloadToKey

const behaviors = {
	[TYPES[SET_LOADED]]: addPayloadToKey(LOADED),
};

is equivalent to :

const behaviors = {
	[TYPES[SET_LOADED]]: (state, { payload }) => ({
	...state,
	[LOADED]: payload,
});

addPayloadToNestedKey

const behaviors = {
	[TYPES[SET_PROFIL_NAME]]: addPayloadToNestedKey(PROFIL, NAME),
};

is equivalent to :

const behaviors = {
	[TYPES[SET_PROFIL_NAME]]: (state, { payload }) => ({
	...state,
	[PROFIL]: {
		...state[PROFIL],
		[NAME]: payload,
	},
});

assignPayloadToState

const behaviors = {
	[TYPES[SET_IS_ONLINE]]: assignPayloadToState,
};

is equivalent to :

const behaviors = {
	[TYPES[SET_IS_ONLINE]]: (state, { payload }) => payload
};

spreadPayloadToState

const behaviors = {
	[TYPES[PATCH_PROFIL]]: spreadPayloadToState,
};

is equivalent to :

const behaviors = {
	[TYPES[PATCH_PROFIL]]: (state, { payload }) => ({
	...state,
	...payload,
});

roadmap :

  • [ ] create more helpers for reducers ( CRUD for collection )