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

reducer

v0.19.2

Published

Implementation-agnostic state reducers for Functional Flux.

Downloads

746

Readme

reducer

Official Reducers for fluxette, and compatible with Redux.

What's a Reducer?

In case you didn't get here from fluxette or Redux, a Reducer is simply a function that takes an aggregate, which we refer to as a state, and a transaction, which we refer to as an action, which it combines, or reduces, to produce a new state. Thus, they all have the signature of (State, Action) => State). Reducers are very useful for state management, a large element of the Flux Architecture.

A fundamental property of Reducers is that they are composable, which allows for orthogonal code, separation of concerns, and easy scaling. This means that one reducer can hold other reducers, which hold even more reducers.

Install

npm install --save reducer

Reducer Creators

Shape(shape) Creates a reducer that holds more reducers on each of its properties. The property names reflect how they will appear on the state.

import Shape from 'reducer/shape';
import { stock, cart } from './reducers';

let store = Shape({ stock, cart });

store()
// { stock: stock(), cart: cart() }

Leaf(initial, reducers) Creates a reducer that applies its reducers to a state respective to the type of the action provided (action.type), using initial as the initial state.

import Leaf from 'reducer/leaf';

let dataReducer = Leaf({ status: 'ready' }, {
	[API_REQUEST]: state => ({ status: 'loading' }),
	[API_DONE]: (state, action) => ({ status: 'done', data: action.data }),
	[API_FAIL]: (state, action) => ({ status: 'error', error: action.error })
});

Filter(types, reducer) Creates a reducer that proxies its action to its reducers only if the action's type matches one of its types.

import Filter from 'reducer/filter';

let userReducer = Filter([USER_LOGIN, USER_LOGOUT, USER_CHAT], user);

History() Creates a reducer that keeps track of the actions that have been dispatched.

import Shape from 'reducer/shape';
import History from 'reducer/history';

let reducer = Shape({
	history: History()
}))

let state;
state = reducer(state, { type: TYPE_A, data: 'a' })
state = reducer(state, { type: TYPE_B, data: 'b' })
state = reducer(state, { type: TYPE_C, data: 'c' })

state.history
// [{ type: TYPE_A, data: 'a' }, { type: TYPE_B, data: 'b' }, { type: TYPE_C, data: 'c' }]

Hydrate(reducer) Creates a reducer that wraps reducer, and replaces the state with action.state if the action is of type Hydrate:type.

import Hydrate, { type as HYDRATE_TYPE } from 'reducer/hydrate';

let reducer = Hydrate(/* other reducer */);

let state;
state = reducer(state, { type: HYDRATE_TYPE, state: { a: 5, b: 6 }});

state
// { a: 5, b: 6 }

SideEffect(reducer, effect) Creates a reducer that wraps reducer, and calls effect as if it were a Reducer, but ignores its return value.

import SideEffect from 'reducer/sideeffect';

let loggingReducer = SideEffect(reducer, (state, action) => console.log(action));

loggingReducer(state, action)

// effectively the same as reducer(state, action), but also logs actions