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

v1.3.7

Published

Redux functional programming utilities

Downloads

65

Readme

redux-fun :wrench:

CircleCI branch codecov npm Greenkeeper badge NSP Status dependencies Status devDependencies Status contributions welcome Commitizen friendly Join the chat at https://gitter.im/redux-fun/Lobby semantic-release

Redux functional programming utilities

Install

$ npm install --save redux-fun

Documentation

Reducers

Compose reducers
  const { composeReducers, pipeReducers } = require('redux-fun');

  const reducer_1 = composeReducers(r3, r2, r1);
  const reducer_2 = pipeReducers(r1, r2, r3);
Convert updaters
const { toReducer } = require('redux-fun');
const updater = (action) => state => state;
const reducer = toReducer(updater);

Updaters

updater with initial state
const { withDefaultState } = require('redux-fun');

// normal
const updater_1 = withDefaultState({}, action => state => state);

// curried
const updater_2 = withDefaultState({})(action => state => state);

Middlewares

Compose middlewares
  const { composeMiddlewares, pipeMiddlewares } = require('redux-fun');

  const middleware_1 = composeMiddlewares(m3, m2, m1);
  const middleware_2 = pipeMiddlewares(m1, m2, m3);
Preserve async flow (deprecated)

Use preserveAsyncSeries instead

Preserve async series

Respect the dispatch order with async middleware

  const { preserveAsyncSeries } = require('redux-fun');
  const middleware_1 = preserveAsyncSeries('ACTION_TYPE')
  const middleware_2 = preserveAsyncSeries(['ACTION_TYPE_1', 'ACTION_TYPE_2'])

Handlers

A Handler from the handle-io library can be tranformed into redux middleware using createHandleIOMiddleware function.

Redux Handlers

A redux handler run every time an action was dispatched.

It takes two arguments:

  • action
  • reduxHandleIOApi

The reduxHandleIOApi object provides 3 IOs functions :

  • getState
  • dispatch
  • next

Please see handle-io documentation for more details.

e.g.

const { io, handler } = require('handle-io');

const sleep = io(s => new Promise(resolve => setTimeout(resolve, s * 1000)));

// delayed action example
const myHandler1 = handler(function*(action, { next }) {
  yield sleep(1);
  return yield next(action);
});

// preserve dispatch order
const myHandler2 = handler(function*(action, { next, dispatch }) {
  const nexted = yield next(action);
  yield dispatch({ type: 'DUMMY_ACTION' });
  return nexted;
});

// print state
const log = io(console.log);
const myHandler3 = handler(function*(action, { next, getState }) {
  const nexted = yield next(action);
  const state = yield getState();
  yield log(state);
  return nexted;
})

Note: A redux handler acts as an async middleware.

testReduxHandler

testReduxHandler is an extension of handle-io#testHandler

it adds 3 match*() methods:

  • .matchNext() - 2 arguments:
    • next argument (assert)
    • next return (mock)
  • .matchDispatch() - 2 arguments:
    • dispatch argument (assert)
    • dispatch return (mock)
  • .matchGetState() - 1 argument:
    • getState return (mock)

e.g.

const { handler } = require('handle-io');
const { testReduxHandler } = require('redux-fun');

const createDummyAction = (payload) => { type: 'DUMMY_ACTION', payload }
const myAction = { type: 'MY_ACTION', payload: {} };

const myHandler4 = handler(function*(action, { dispatch, getState, next }) {
  const { value } = yield getState();
  yield dispatch(createDummyAction(value));
  return yield next(myAction);
})

testReduxHandler(myHandler4, myAction)
  .matchGetState({ value: 42 })
  .matchDispatch(createDummyAction(42))
  .matchNext(myAction, 'returned value')
  .shouldReturn('returned value')
  .run()

createHandleIOMiddleware

usage:

const { createHandleIOMiddleware } = require('redux-fun');
const handleIOMiddleware = createHandleIOMiddleware(myHandler1, myHandler2, myHandler3, myHandler4)

Selectors

Bind selectors
  const { bindSelectors } = require('redux-fun');

  const selectors = {
    getUsers: (state) => state.users,
    getUser: (state, { id }) => state.users[id],
  }

  const boundSelectors = bindSelectors(selectors, store.getState);
  const user = boundSelectors.getUser({ id: 1 });
getOwnProp

getOwnProp is very useful to use with createSelector from reselect

const { createSelector } = require('reselect');
const { getOwnProp } = require('redux-fun');

const getUsers = (state = {}) => state.users || {}

const getUserByName = createSelector(
  getUsers,
  getOwnProp('fullName'),
  (users, fullName) => users[fullName]
)

getUserByName({ users: { user1: true } }, { fullName: 'user1' }); // => true