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

immutable-reducers

v1.1.0

Published

Create reducers for immutable data structures. Useful for redux.

Downloads

6

Readme

immutable-reducers

semantic-release travis-ci

Create reducers for immutable data structures. Useful for redux.

Table of contents

Install

npm install --save immutable-reducers

Use

import { fromJS } from 'immutable';
import { combineReducers, createReducer } from 'immutable-reducers';

// Setup some state (probably your app state)

const initialState = fromJS({
    artist: {
        name: {
            first: 'Sean',
            last: 'Combs'
        },
        fans: 0
    }
});

// Create some reducers

const artistNameReducer = createReducer(['artist', 'name'], (state, action) => {
    switch (action.type) {
        case 'RENAME':
            return fromJS(action.value);
    }
    return state;
});

// You can scope them by combining with an object

const artistFansReducer = createReducer(['artis', 'fans'], (state, action) => {
    switch (action.type) {
        case 'NEW_FAN':
            return state + action.count;
    }
    return state;
});

// Combine 'em up

const reducer = combineReducers(artistNameReducer, artistFansReducer);

// Step the state

reducer(initialState, { type: 'NEW_FAN', count: 1 });

createReducer

createReducer helps you make a reducer that operates on a small area of an immutable data structure.

createReducer(
    keyPath: Array<any>,
    updater: (targetState: any, action: Object) => any,
    state: any
): any

For example, given some initial state:

const initialState = fromJS({
    user: {
        favorites: Immutable.OrderedSet()
    }
});

We can create a reducer that looks out for favorite actions and remembers the id:

const favoriteReducer = createReducer(['user', 'favorites'], (favorites, action) => {
    switch (action.type) {
        case 'FAVORITE':
            return favorites.add(action.id);
    }
    return favorites;
});

createReducer handles reaching into the data structure and updating the value.

Good to know:

  • If the updater function returns the same value it was called with, then no change will occur.
  • If the keyPath you specify does not exist, an Immutable Map will be created at each intermediary key.
  • The keys can be immutable data structures too #winning
  • The third argument to the reducer is the whole state object, allowing you consult other areas of state when handling an action

combineReducers

combineReducers is a less opinionated version of redux's default combineReducers utility.

type Reducer = (state: any, action: Object) => any;
type ReducerObject = Object<string, Reducer>;

combineReducers(...Reducer|ReducerObject): Reducer

It has two useful forms: applied to a list of reducers or to an object. When applied to the list, it acts like redux's combineReducers.

When applied to an object, it's slightly different. immutable-reducers will use the key of the object to 'scope' the reducer further.

For example, in the following example: You could scope it to the user key of your (immutable) state using combineReducers:

const combineReducer = combineReducers({
    user: createReducer(['favorites'], (favorites, action) => {
        switch (action.type) {
            case 'FAVORITE':
                return favorites.add(action.id);
        }
        return favorites;
    });
});

The end result is the same as the createReducers example, where the favoritesReducer will actually operate on the ['user', 'favorites'] key path.

Contributing

Please read the contribution guidelines. Contributions are welcome!

Thanks

Thanks to rackt for redux and all those who work on immutable.

License

Copyright (c) 2015 Tom Ashworth. Released under the MIT license.