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

es-reduce-list

v0.0.1

Published

Enables a reducer to reduce multiple items, useful for lists of objects.

Downloads

1

Readme

Reduce List

This module makes it easy to convert a reducer that reduces a single object, and use it to reduce a list of objects. You can specify which actions are applied to all objects, and which ones are applied only to specific objects.

Install

To install: $ npm install es-reduce-list

Usage

Let's say we are building out a TodoMVC, and we have built the following reducer to reduce a single Todo item:

import {
  TOGGLE_COMPLETE,
  EDIT_TEXT,
} from 'actions';

const reduceTodo = (state, action) => {
  switch (action.type) {
  
    case TOGGLE_COMPLETE:
      return {
        ...state,
        complete: !state.complete,
      };
      
    case EDIT_TEXT:
      return {
        ...state,
        text: action.payload,
      };
      
  }
};

We can toggle whether the todo item is complete or not, as well as edit the todo.

But now we want to be able to reduce a list of items, not just one. We can do this by using reduceList:

import reduceList from 'es-reduce-list';

const reduceTodoList = (state = {}, action) => {
  return reduceList(
    state,
    action,
    reduceTodo,
    [ TOGGLE_COMPLETE, EDIT_TEXT ],
    (action) => action.id,
  );
};

We first pass in state, the action, and reduceTodo to the function. Next, we have to pass in an array of actions that are 'single-item' actions, meaning that they target a specific item (they should not be applied to the entire list). All other actions will be applied to each item in the list. Lastly, we provide a function that we can use to get the id of the targeted item from the action.

Our state must be an object, where each key is the id of the item, and each item has a property id which is the same as the key. For our todo list it would look like:

{
  0: { id: 0, text: 'Todo 1', completed: false },
  1: { id: 1, text: 'The last thing I want to do', completed: false },
  ...
}

Our action must also have the id of the targeted item somewhere in it:

{
  type: 'TOGGLE_COMPLETE',
  id: 1,
}

If we wanted to have a COMPLETE_ALL action, we would first update the reduceTodo reducer:

import {
  TOGGLE_COMPLETE,
  EDIT_TEXT,
  COMPLETE_ALL,
} from 'actions';

const reduceTodo = (state, action) => {
  switch (action.type) {
  
    case TOGGLE_COMPLETE:
      return {
        ...state,
        complete: !state.complete,
      };
      
    case EDIT_TEXT:
      return {
        ...state,
        text: action.payload,
      };
      
    case COMPLETE_ALL:
      return {
        ...state,
        complete: true,
      };
      
  }
};

...and that's it! Because it's not specified in our list of 'single-item' actions, it will be applied to every single item in the list.

But how would we add new todos?

We can modify reduceTodoList like so:

import reduceList from 'es-reduce-list';

const reduceTodoList = (state = {}, action) => {
  if (action.type === ADD_NEW_TODO) {
    return {
      ...state,
      [action.payload.id]: action.payload,
    };
  } else {
    return reduceList(
      state,
      action,
      reduceTodo,
      [ TOGGLE_COMPLETE, EDIT_TEXT ],
      (action) => action.id,
    );
  }
};